leveldown: Close callback never called

I have a code in which callback is not being called:

var level = require('level')

module.exports = function (databaseDir, input, callback) {
  var db = level(databaseDir, function () {
    console.log('a')
    input.del.forEach(db.del.bind(db))
    for (var key in input.put) {
      db.put(key, input.put[key])
    }
    console.log('b')
    db.close(function () {
      console.log('c')
      callback()
    })
  })
}

input contains a quite a few put keys.

The output is

a
b

so… c is never reached. I tried to find the problem but, well…

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 22 (14 by maintainers)

Most upvoted comments

Looks like the function type should be checked?!

An invalid argument should throw, imo. It would be hard to track down bugs otherwise.

More importantly, the issue of closing early still stands. There’s currently no guarantee the data is actually written to disk, if I’m reading it right.

I understand what you’re trying to do, and I’m not saying the segfault bug isn’t a valid concern, but it looks to me as if you’re using close() for async flow control here. Other mechanisms (like batch or some flow control lib) would be more appropriate.

@michaelnisi that’s because this line of code:

input.del.forEach(db.del.bind(db))

Is effectively:

input.del.forEach(function(key, index, array) {
  db.del(key, index, array)
})