Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ var EventEmitter = require('events').EventEmitter
, defaultOptions = util.defaultOptions
, getLevelDOWN = util.getLevelDOWN
, dispatchError = util.dispatchError
, isDefined = util.isDefined

function getCallback (options, callback) {
return typeof options == 'function' ? options : callback
}

// Possible LevelUP#_status values:
// - 'new' - newly created, not opened or closed
Expand Down Expand Up @@ -84,6 +79,8 @@ function LevelUP (location, options, callback) {
// set this.location as enumerable but not configurable or writable
prr(this, 'location', location, 'e')

this._activeOperations = 0

this.open(callback)
}

Expand Down Expand Up @@ -132,7 +129,9 @@ LevelUP.prototype.open = function (callback) {
LevelUP.prototype.close = function (callback) {
var self = this

if (this.isOpen()) {
if (this._activeOperations > 0) {
this.once('inactive', this.close.bind(this, callback))
} else if (this.isOpen()) {
this._status = 'closing'
this.db.close(function () {
self._status = 'closed'
Expand Down Expand Up @@ -193,12 +192,25 @@ function readError (db, message, callback) {
return true
}

function guardCallback (db, options, callback) {
var callbackFunction = typeof options == 'function' ? options : callback
if (typeof callbackFunction != 'function')
return callbackFunction

db._activeOperations++

return function levelupCallback () {
if (--db._activeOperations === 0)
db.emit('inactive')
callbackFunction.apply(this, arguments)
}
}

LevelUP.prototype.get = function (key_, options, callback) {
var self = this
, key

callback = getCallback(options, callback)
callback = guardCallback(this, options, callback)

if (maybeError(this, options, callback))
return
Expand Down Expand Up @@ -238,7 +250,7 @@ LevelUP.prototype.put = function (key_, value_, options, callback) {
, key
, value

callback = getCallback(options, callback)
callback = guardCallback(this, options, callback)

if (key_ === null || key_ === undefined)
return writeError(this, 'put() requires a key argument', callback)
Expand All @@ -265,7 +277,7 @@ LevelUP.prototype.del = function (key_, options, callback) {
var self = this
, key

callback = getCallback(options, callback)
callback = guardCallback(this, options, callback)

if (key_ === null || key_ === undefined)
return writeError(this, 'del() requires a key argument', callback)
Expand All @@ -289,14 +301,12 @@ LevelUP.prototype.del = function (key_, options, callback) {

LevelUP.prototype.batch = function (arr_, options, callback) {
var self = this
, keyEnc
, valueEnc
, arr

if (!arguments.length)
return new Batch(this, this._codec)

callback = getCallback(options, callback)
callback = guardCallback(this, options, callback)

if (!Array.isArray(arr_))
return writeError(this, 'batch() requires an array argument', callback)
Expand All @@ -323,7 +333,7 @@ LevelUP.prototype.approximateSize = deprecate(function (start_, end_, options, c
, start
, end

callback = getCallback(options, callback)
callback = guardCallback(this, options, callback)

options = getOptions(options)

Expand Down Expand Up @@ -392,4 +402,3 @@ module.exports.repair = deprecate(
utilStatic('repair')
, 'levelup.repair() is deprecated. Use leveldown.repair() instead'
)

40 changes: 40 additions & 0 deletions test/close-racecondition-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Copyright (c) 2012-2015 LevelUP contributors
* See list at <https://github.com/level/levelup#contributing>
* MIT License <https://github.com/level/levelup/blob/master/LICENSE.md>
*/

var levelup = require('../lib/levelup.js')
, common = require('./common')

, assert = require('referee').assert
, refute = require('referee').refute
, buster = require('bustermove')


buster.testCase('Close', {
'setUp': common.commonSetUp
, 'tearDown': common.commonTearDown
, 'several put() operations followed by a close()': function(done) {
this.openTestDatabase(function(db) {
var putsCompleted = 0
db.put('some key 1', 'some value 1 stored in the database', function (err) {
putsCompleted++
refute(err)
})
db.put('some key 2', 'some value 2 stored in the database', function (err) {
putsCompleted++
refute(err)
})
db.put('some key 3', 'some value 3 stored in the database', function (err) {
putsCompleted++
refute(err)
})
db.close(function(err) {
assert(db._activeOperations === 0, 'close called while there are still active operations')
assert(putsCompleted === 3, 'close completed before put operations')
refute(err)
done()
})
}.bind(this))
}
})