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

Fix(session): race condition crash #486

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -73,6 +73,15 @@ SessionStore.prototype.createSession = function(sid, fn) {
var defer = Q.defer();
if (!session) {
store.insert({ id: sid }, function (err, s) {
if (err && err.name === 'MongoError' && err.code === 11000) {
// E11000 is duplicate key error index in mongodb, some other request already
// inserted this session, try finding it again (race condition fix)
store.find({ id: sid }, function (err, s) {
if (err) return defer.reject(err);
defer.resolve(s);
});
return;
}
if (err) return defer.reject(err);
defer.resolve(s);
});
@@ -82,6 +91,8 @@ SessionStore.prototype.createSession = function(sid, fn) {
return defer.promise;
})
.then(function(s) {
if (!s) throw "Could not find session";

var sess = sessionIndex[sid] || new Session(s, store, socketIndex, store.sockets);
sessionIndex[sid] = sess;
// index sessions by user
@@ -92,8 +103,7 @@ SessionStore.prototype.createSession = function(sid, fn) {
})
.catch(function (err) {
fn(err); // couldn't get a session
})
.done();
});
} else {
sid = this.createUniqueIdentifier();
var sess = sessionIndex[sid] = new Session({id: sid}, this, socketIndex, store.sockets);
@@ -1,6 +1,7 @@
var SessionStore = require('../lib/session').SessionStore
, db = require('../lib/db')
, EventEmitter = require('events').EventEmitter;
, EventEmitter = require('events').EventEmitter
, sinon = require('sinon');

describe('SessionStore', function() {
it('should bind sockets to sessions', function() {
@@ -72,6 +73,15 @@ describe('Session', function() {
fn(err, session);
});
}

beforeEach(function () {
this.sinon = sinon.sandbox.create();
});

afterEach(function () {
this.sinon.restore();
});


it('should make sockets available even before they exist', function(done) {
this.timeout(100);
@@ -96,6 +106,29 @@ describe('Session', function() {
});
});

it('should not crash process when inserting the same session to the database', function (done) {
var store = new SessionStore('sessions', db.create(TEST_DB));
var originalFind = store.find;
store.find = sinon.stub(store, "find", function () {
var myArgs = arguments;
// simulate 10 ms latency for find function
setTimeout(function() { originalFind.apply(store, myArgs); }, 10);
});

var callsLeft = 5;
function callback(err) {
expect(err).to.not.exist;
callsLeft--;
if (callsLeft == 0) return done();
}

for (var i = 0; i< 5; i++) {
setTimeout(function() {
store.createSession("1c829fa9ed135d0301919aa70505cf36a39b35527b775e45e1523c354e0c5f2b0b1636f173a94e26082291ae9ad0e3e74cb5226bd0aa86ee9f7a3749d57cc74d", callback);
}, 1);
}
});

describe('.set(changes)', function() {
it('should set the changes to a sessions data', function(done) {
createSession(function (err, session) {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.