Pm2: Memory leaks when pm2 updated to 3.4.0 or later version

Created on 2 Jun 2019  路  15Comments  路  Source: Unitech/pm2

Memory leaks in 3.4.0 and later versions

each request will be an instance of new Meter.js

Call chain

  • node_modules/@pm2/io/build/main/index.js
const pmx_1 = require("./pmx");
const IO_KEY = Symbol.for('@pm2/io');
const isAlreadyHere = (Object.getOwnPropertySymbols(global).indexOf(IO_KEY) > -1);
  • node_modules/@pm2/io/build/main/pmx.js
        this.actionService.init();
        serviceManager_1.ServiceManager.set('actions', this.actionService);
        this.metricService = new metrics_1.MetricService();
  • node_modules/@pm2/io/build/main/metrics/httpMetrics.js
    hookRequire() {
        this.hooks = requireMiddle(['http', 'https'], (exports, name) => {
            this.hookHttp(exports, name);
            return exports;
        });
    }

```javascript
if (event !== 'request')
return original.apply(this, arguments);
if (name === 'http') {
self.registerHttpMetric();
}

* node_modules/@pm2/io/build/main/utils/metrics/meter.js
```javascript
    this._interval = setInterval(function () {
      self._rate.tick();
    }, this._tickInterval);
    this._interval.unref();

Simple test case

//test.js
const http = require('http');
http.createServer(function(req, res){
  res.end('okay');
}).listen(8080)

1銆乺un by ./node_modules/.bin/pm2-dev start ./test.js

2銆乺equest http://127.0.0.1:8080

as the amount of requests increases, the cpu will eventually skyrocket and memory leaks

Most helpful comment

is there any update to fix this?

All 15 comments

I also encountered this problem when updated to 3.5.1.

Is that with @pm2/io >= 4.2.2?

Is that with @pm2/io >= 4.2.2?


Seems to be >=4.2.1

no problem

private hookRequire () {
    //...
    shimmer.wrap(Module, '_load', function (original: Function) {
      return function (file) {
        // we first require the target module
        const returned = original.apply(original, arguments)
        // if it's a modue that we want to patch, install the patch on top of it
        if (file === 'http' && self.modules['http'] === undefined) {
          self.registerHttpMetric()
          self.hookHttp(returned, file)
        } else if (file === 'https' && self.modules['https'] === undefined) {
          self.registerHttpsMetric()
          self.hookHttp(returned, file)
        }
        // and of course send the module so user can use it
        return returned
      }
    })
    //...
  }

problematic

 private hookRequire () {
    this.hooks = requireMiddle(['http', 'https'], (exports, name) => {
      this.hookHttp(exports, name)
      return exports
    })
  }

You can reproduce with 4.2.2?

I also encountered this problem when updated to [email protected].

I encountered the problem when I updated to 3.5.1, but can't seem to reproduce with 3.5.0.

Version of pm2 doesn't really matter as the problem is/was in @pm2/io package and installing any recent version of pm2 always installs latest version of @pm2/io.

The bug might be fixed already in latest version of @pm2/io btw. I'm still verifying that.

Is there a way to disable the HTTP and HTTPS intercepts completely? This is causing memory and CPU runaway in production at the moment

Memory leak is fixed as long as your pm2 is using @pm2/io >= 4.2.2

Also this problem when updated to [email protected], downgrade to [email protected] fixed the leak for now

I am still seeing CPU and Memory usage stair-step up even on 4.2.3 - going to try downgrading [email protected] to see if it fixes this.

@hodoublesy, did that help you ?

@maxfadeev - yes, after downgrading to 3.2.2 everything has been working great. I've had runaway memory leak issues in the past upgrading to the latest pm2 so I'll just be cautious in the future upgrading!

is there any update to fix this?

This has been fixed with @pm2/io 4.2.2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mario-mui picture mario-mui  路  3Comments

rajendar38 picture rajendar38  路  3Comments

phra picture phra  路  3Comments

rangercyh picture rangercyh  路  4Comments

morfies picture morfies  路  3Comments