Skip to content

"complete" callback of Database.each() leaking memory? #297

Closed
@thierer

Description

@thierer

I've got a memory leak in the complete callback of Database.each() that I can't explain.

This is running node-sqlite3 2.2.3 and node 0.10.22 on Linux 3.12.13 x86_64.

The following stripped down example shows the behaviour:

var sqlite3 = require('sqlite3');

function doLeak(callback) {
  var leak = new Buffer(1000000);

  db.each('SELECT 1',
    function item(err, row) {
    },
    function complete(err, found) {
      callback(leak);
    }
  );
}

var db = new sqlite3.Database(':memory:', sqlite3.OPEN_READONLY);
db.once('open', function(err) {
  if (err) return console.error(err);

  var active = 0;

  (function callLeak() {
    active++;
    doLeak(function() {
      console.log(--active);
      callLeak();
    });
  })();
});

The Buffer allocation is just there to increase the effect. If I use heapdump to take snapshots, I can see the "complete" functions pile up in the "Containment" view of Devtools. (Note that it only seems to affect the completeand not the item callback).

I'm not sure if it's really a bug or just my lack of understanding javascript / node.js / v8 / whatever.

OTOH, I don't see any leaks if I replace the sqlite3 query and use the same call pattern with fs.readFile(), which I would consider a comparable situation?

var fs = require('fs');

function doLeak(callback) {
  var leak = new Buffer(1000000);

  fs.readFile('test.txt',
    function complete(err, data) {
      callback(leak);
    }
  );
}

var active = 0;

(function callLeak() {
  active++;
  doLeak(function() {
    console.log(--active);
    callLeak();
  });
})();

Any help would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions