Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition #5

Open
nolanlawson opened this issue Jul 5, 2014 · 1 comment
Open

Race condition #5

nolanlawson opened this issue Jul 5, 2014 · 1 comment

Comments

@nolanlawson
Copy link
Member

I am 99% sure there is a race condition in this module. If you run npm run test-pouchdb in pouchdb-server enough times, you'll see stuff like this:

Possibly unhandled Error: database is closed
    at applyNextDocCountDelta (/Users/nolan/workspace/pouchdb-server/node_modules/pouchdb/lib/adapters/leveldb.js:120:28)
    at incrementDocCount (/Users/nolan/workspace/pouchdb-server/node_modules/pouchdb/lib/adapters/leveldb.js:142:5)
    at countDocs (/Users/nolan/workspace/pouchdb-server/node_modules/pouchdb/lib/adapters/leveldb.js:107:14)
    at PouchAlt.LevelPouch.api._info (/Users/nolan/workspace/pouchdb-server/node_modules/pouchdb/lib/adapters/leveldb.js:154:5)
    at PouchAlt.<anonymous> (/Users/nolan/workspace/pouchdb-server/node_modules/pouchdb/lib/adapter.js:666:8)
    at PouchAlt.<anonymous> (/Users/nolan/workspace/pouchdb-server/node_modules/pouchdb/lib/utils.js:443:21)
    at PouchAlt.<anonymous> (/Users/nolan/workspace/pouchdb-server/node_modules/pouchdb/node_modules/argsarray/index.js:14:18)
    at /Users/nolan/workspace/pouchdb-server/node_modules/pouchdb/lib/utils.js:405:21
    at tryCatch2 (/Users/nolan/workspace/pouchdb-server/node_modules/pouchdb/node_modules/bluebird/js/main/util.js:73:19)
    at Promise$_resolveFromResolver [as _resolveFromResolver] (/Users/nolan/workspace/pouchdb-server/node_modules/pouchdb/node_modules/bluebird/js/main/promise.js:595:13)
@gr2m
Copy link

gr2m commented Aug 14, 2017

despite the issue being old, just want to mention that the problem here is that this module executes asynchronous code in event handlers:

Pouch.on('created', function (dbName) {
dbName = normalize(dbName);
if (canIgnore(dbName)) {
return;
}
dbName = prefixed(dbName);
init();
queue.add(function (callback) {
pouch.get(dbName).then(function () {
// db exists, nothing to do
}).catch(function (err) {
/* istanbul ignore if */
if (err.name !== 'not_found') {
throw err;
}
return pouch.put({_id: dbName});
}).then(function () {
if (cache) {
cache[dbName] = true;
}
callback();
}, callback);
}, log);
});
Pouch.on('destroyed', function (dbName) {
dbName = normalize(dbName);
if (canIgnore(dbName)) {
return;
}
dbName = prefixed(dbName);
init();
queue.add(function (callback) {
pouch.get(dbName).then(function (doc) {
return pouch.remove(doc);
}).catch(/* istanbul ignore next */ function (err) {
// normally a not_found error; nothing to do
if (err.name !== 'not_found') {
throw err;
}
}).then(function () {
/* istanbul ignore else */
if (cache) {
delete cache[dbName];
}
callback();
}, callback);
}, log);
});
This is what can cause race condition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants