ogesh
ogesh

Reputation: 3

Nodejs closure issue

I am using express-session with RedisStore in my node web application.

NodeJs version: 4.2.4 JS: Es6

"express": "^4.13.3", "express-session": "https://github.com/echorohit/session/tarball/1d22749aa46280a24a4ee3a993ba0772e6e1e2df", "ioredis": "^1.13.0", "connect-redis": "^3.0.1",

I observed that once in a while the session are getting mixed up between users. I was using newrelic insights for logging some metrics so I used that to debug the issue also.

Here are my findings.

code snippet from store.js of express-session

 Store.prototype.regenerate = function(req, fn){
     var self = this;
     //CHECKPOINT 1
     log.addNewRelicCustomParameter("CHECKPOINT1",req.sessionID);
     this.destroy(req.sessionID, function(err){
        //CHECKPOINT 2
        log.addNewRelicCustomParameter("CHECKPOINT2",req.sessionID);
        self.generate(req, fn);
        fn(err);
     });
};

At CHECKPOINT 1. The req.sessionID was correct but strangely at CHECKPOINT 2. the req.sessionID was printed for other request being concurrently executed in new relic logs.

I am sure this is the cause of session mixup. But not able to debug further to find root cause of it. Please help me with tips how to get to the root cause of problem. Not sure if this is really closure scope problem.

Upvotes: 0

Views: 88

Answers (1)

cybersam
cybersam

Reputation: 67019

It looks like there is actually no problem!

If you set a breakpoint at each of your checkpoints, it is to be expected that sometimes the req.sessionID will differ between the breakpoints, since the second breakpoint is inside an asynchronous callback.

Upvotes: 0

Related Questions