Skip to content

Pooling + Memory Leaks #80

Description

@davisford

Hiya, I'm seeing some serious leakage going on when using the pool feature. I may very well be using the API wrong, but can you take a quick look?

Here's what's going on with htop (showing threads) under normal conditions (after doing a few print jobs):

 6969 ubuntu     20   0  679M 23440  5104 S  0.0  0.6  0:00.41 ├─ pm2: Daemon
20465 ubuntu     20   0  967M 51288  6772 S  0.0  1.3  0:00.83 │  ├─ node /home/ubuntu/git/warehouse-print/app.js
20493 ubuntu     20   0 2146M  320M 19344 S  0.0  8.5  0:06.33 │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stre
23500 ubuntu     20   0 2146M  320M 19344 S  0.0  8.5  0:00.02 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20593 ubuntu     20   0 2146M  320M 19344 S  0.0  8.5  0:00.01 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20568 ubuntu     20   0 2146M  320M 19344 S  0.0  8.5  0:00.01 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20527 ubuntu     20   0 2146M  320M 19344 S  0.0  8.5  0:00.02 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20511 ubuntu     20   0 2146M  320M 19344 S  0.0  8.5  0:00.02 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20500 ubuntu     20   0 2146M  320M 19344 S  0.0  8.5  0:00.00 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20497 ubuntu     20   0 2146M  320M 19344 S  0.0  8.5  0:00.03 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20496 ubuntu     20   0 2146M  320M 19344 S  0.0  8.5  0:00.00 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20495 ubuntu     20   0 2146M  320M 19344 S  0.0  8.5  0:00.00 │  │  │  └─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20492 ubuntu     20   0  967M 51288  6772 S  0.0  1.3  0:00.00 │  │  ├─ node /home/ubuntu/git/warehouse-print/app.js
20491 ubuntu     20   0  967M 51288  6772 S  0.0  1.3  0:00.00 │  │  ├─ node /home/ubuntu/git/warehouse-print/app.js
20490 ubuntu     20   0  967M 51288  6772 S  0.0  1.3  0:00.00 │  │  ├─ node /home/ubuntu/git/warehouse-print/app.js
20489 ubuntu     20   0  967M 51288  6772 S  0.0  1.3  0:00.00 │  │  ├─ node /home/ubuntu/git/warehouse-print/app.js
20466 ubuntu     20   0  967M 51288  6772 S  0.0  1.3  0:00.02 │  │  └─ node /home/ubuntu/git/warehouse-print/app.js
 7008 ubuntu     20   0  679M 23440  5104 S  0.0  0.6  0:00.00 │  ├─ pm2: Daemon
 7007 ubuntu     20   0  679M 23440  5104 S  0.0  0.6  0:00.00 │  ├─ pm2: Daemon
 7006 ubuntu     20   0  679M 23440  5104 S  0.0  0.6  0:00.00 │  ├─ pm2: Daemon
 7005 ubuntu     20   0  679M 23440  5104 S  0.0  0.6  0:00.00 │  ├─ pm2: Daemon
 6970 ubuntu     20   0  679M 23440  5104 S  0.0  0.6  0:00.01 │  └─ pm2: Daemon

Now, I run a few more print jobs:

36816  6136  1528 S  0.0  0.2  0:01.38 /sbin/init
 6969 ubuntu     20   0  679M 23536  5104 S  0.0  0.6  0:00.46 ├─ pm2: Daemon
20465 ubuntu     20   0  970M 55028  6772 S  0.0  1.4  0:01.05 │  ├─ node /home/ubuntu/git/warehouse-print/app.js
20493 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:14.16 │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stre
29349 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.01 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
29341 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.01 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
29336 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.00 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
29331 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.02 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
29322 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.02 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
29314 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.02 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
29296 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.02 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
29285 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.02 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
23500 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.02 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20593 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.01 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20568 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.01 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20527 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.02 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20511 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.02 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20500 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.00 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20497 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.03 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20496 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.00 │  │  │  ├─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20495 ubuntu     20   0 2871M  636M 19344 S  0.0 16.9  0:00.00 │  │  │  └─ /home/ubuntu/git/warehouse-print/node_modules/phantom-render-stream/node_modules/phantomjs/lib/phantom/bin/phantomjs --disk-cache=true /home/ubuntu/git/warehouse-print/node_modules/phantom-render-s
20492 ubuntu     20   0  970M 55028  6772 S  0.0  1.4  0:00.00 │  │  ├─ node /home/ubuntu/git/warehouse-print/app.js
20491 ubuntu     20   0  970M 55028  6772 S  0.0  1.4  0:00.00 │  │  ├─ node /home/ubuntu/git/warehouse-print/app.js
20490 ubuntu     20   0  970M 55028  6772 S  0.0  1.4  0:00.01 │  │  ├─ node /home/ubuntu/git/warehouse-print/app.js
20489 ubuntu     20   0  970M 55028  6772 S  0.0  1.4  0:00.00 │  │  ├─ node /home/ubuntu/git/warehouse-print/app.js
20466 ubuntu     20   0  970M 55028  6772 S  0.0  1.4  0:00.02 │  │  └─ node /home/ubuntu/git/warehouse-print/app.js
 7008 ubuntu     20   0  679M 23536  5104 S  0.0  0.6  0:00.00 │  ├─ pm2: Daemon
 7007 ubuntu     20   0  679M 23536  5104 S  0.0  0.6  0:00.00 │  ├─ pm2: Daemon
 7006 ubuntu     20   0  679M 23536  5104 S  0.0  0.6  0:00.00 │  ├─ pm2: Daemon
 7005 ubuntu     20   0  679M 23536  5104 S  0.0  0.6  0:00.00 │  ├─ pm2: Daemon
 6970 ubuntu     20   0  679M 2

The number of phantom instances has increased and it doesn't seem to go back down. Over a longer period of time (~ couple weeks), eventually I exhaust the physical RAM and the machine has to be reset.

I'm using the API like so:

var render = phantom({ 
  /* other options */
  pool: 5
});

// I'm uploading the pdfs to Amazon S3 via streams (that is what `upload` is):

var upload = s2stream.upload({ /* options */ });
var compress = zlib.createGzip();

upload.on('error', function (err) {
  log.error(err);
  done(err);
});

upload.on('uploaded', function (info) {
  log.info(info);
  done (null, info);
});

render(url, {
  /* options */
}).pipe(compress)
  .pipe(upload);

If I do render.destroy( ) like this:

upload.on('error', function (err) { render.destroy(); done(err); });
upload.on('uploaded', function (info) { render.destroy(); done(); });

It seems to clean up and not have the leaks, but it also doesn't seem to keep the pool available, so on every new print job, it spins up 5 more instances of phantom, processes via one of them, and then shuts it all down. In essence, I can stop the leak from happening, but I no longer have the advantage of using the pool at all.

So, I'm wondering if I'm using the API wrong, or is there another issue here?

I do have a fork of your repo that is older and not up to date with the latest, so I will investigate a fetch upstream and see what has changed, but I wanted to just pose the question here.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions