Node session not saving correctly
<p>I'm seeing something very odd with node sessions. I'm using <a href="https://github.com/expressjs/session" rel="noreferrer">express-session</a> and <a href="https://github.com/jdesboeufs/connect-mongo" rel="noreferrer">connect-mongo</a> to save the session. </p>

<p>My session setup is as follows (all requests are over https):</p>

<pre><code>var session = require('express-session');
var MongoStore = require('connect-mongo/es5')(session);

var sess = {};

app.use(session({
    secret: 'xxxx',
    saveUninitialized: false,
    store: new MongoStore({
        mongooseConnection: mongoose.connection,
        ttl: 60 * 30 // haf hour
    }),
    cookie: { secure: true }
}));
</code></pre>

<p>I have a route that creates an image with node canvas:</p>

<pre><code>app.get('/api/canvas', function(req, res) {

    sess = req.session;

    console.log('in canvas and sess is ', sess);

    // create image stuff
});
</code></pre>

<p>Elsewhere in the app, you can set stuff like background colors. I then have a route to clear these:</p>

<pre><code>app.get('/api/clear', function(req, res) {

    sess = req.session;

    if (sess.colors) {
        delete sess.colors;
    }

    sess.save(function(err) {
        console.log('saved sess is now ', sess);
        // session saved
        helper.sendJsonResponse(req, res, 200, {});
    })
});
</code></pre>

<p><code>helper.sendJsonResponse()</code> just responds with a <strong>200</strong> HTTP code and an empty body.</p>

<p>You can see I use a callback for the <code>session.save()</code> as I only want to respond to the request when I KNOW the session has been edited and saved. </p>

<p>However, this doesn't always work. If I set colors in the session, then clear them (by calling the 'api/clear' route), then call the route to create the image (by calling 'api/canvas' route), the session on the 'api/canvas' route session sometimes still has the colors set.</p>

<p>This seems to be the case only if I do it quickly. If I wait a few seconds, the colors are cleared for the 'api/canvas' route. </p>

<p>Note: I do not allow calls to these routes at the same time - I use promises and have a spinner in the UI which covers the whole screen until requests fully complete.</p>

<p>When the this happens, the logs look like:</p>

<pre><code>saved sess is now  { cookie: 
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true },
  canvasHeight: 500,
  canvasWidth: 591 
}
</code></pre>

<p>And when I make request to 'api/canvas':</p>

<pre><code>in canvas and sess is  { cookie: 
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true },
  canvasHeight: 500,
  canvasWidth: 591,
  colors: 
   { 
      color1: 'red',
      color2: 'green'
    } 
}
</code></pre>

<p>so you can see, colors property is still set. This happens about 50% of the time. The rest of the time, on the second request the colors property has been deleted.</p>

<p>What could possibly be going wrong? Am I misunderstanding how sessions work?</p>

<p><strong>EDIT</strong></p>

<p>This gets ever odder. If I chain the calls in the UI, that is called the 'api/clear' route then in the Promise then() method, call the 'api/canvas' route, it ALWAYS works. If I call to make the requests individually, the results are completely random - sometimes the session will get updated, sometimes it won't. </p>

<p>I checked <code>req.sessionID</code> and it's always the same for the different requests.</p>

<p><strong>EDIT</strong>
Using the <a href="https://github.com/expressjs/session#sessionregeneratecallback" rel="noreferrer">regenerate method</a> works correctly, but isn't ideal as I lose everything in the session.</p>