diff --git a/lib/hooks/sockets/lib/interpreter/saveSessionAndThen.js b/lib/hooks/sockets/lib/interpreter/saveSessionAndThen.js index f93cddee1..3d15657ae 100644 --- a/lib/hooks/sockets/lib/interpreter/saveSessionAndThen.js +++ b/lib/hooks/sockets/lib/interpreter/saveSessionAndThen.js @@ -15,12 +15,19 @@ module.exports = function saveSessionAndThen(req, sails, cb) { return function saveSession () { var ctx = this, args = Array.prototype.slice.call(arguments); - - req.session.save(function (err) { + var next = function (err) { if (err) { sails.log.error('Session could not be persisted:',err); } cb.apply(ctx,args); - }); + }; + + // If we have a session on the request, save it + // Otherwise, process the callback on the next tick + if (req.session) { + req.session.save(next); + } else { + process.nextTick(next); + } }; };