title: Cannot set session variables from child process callbacks
body: <p>I have a node/express application which calls an external program and needs to capture output and handle different possible states.</p>
  
  <p>The problem is: I can't set the session state inside the on(data) callback.</p>
  
  <p>The function handling the call is:</p>
  
  <pre><code>app.get('/test/:script', function(req, res) {
     if (req.session.state != SessionState.STARTED) {
        req.session.state = SessionState.STARTED;
        req.session.scriptname = req.params.script;
        req.session.outputText = '';
  
        var test = spawn('./Test', [ req.session.scriptname ]);
  
        test.stdout.on('data', function(data) { console.log('+++' + JSON.stringify(req.session)); req.session.outputText += data.toString() + '&lt;br/&gt;'; });
        test.on('close', function(code) { console.log('---' + JSON.stringify(req.session)); console.log(code); req.session.state = SessionState.FINISHED; });
     }
  
     res.redirect(301, '/');
  })
  </code></pre>
  
  <p>And the default route is:</p>
  
  <pre><code>app.get('/', function (req, res) {
     var responseBody = '';
     var responseHead = '';
     switch (req.session.state) {
        case undefined:
        case SessionState.IDLE:
           var files = fs.readdirSync('../input')
           for (var i = 0; i &lt; files.length; i++) {
              var fname = files[i];
              responseBody += '&lt;a href="/test/' + fname + '"&gt;' + fname + '&lt;/a&gt; &lt;br/&gt;';
           }
           req.session.state = SessionState.IDLE;
           break;
  
        case SessionState.STARTED:
           responseHead += '&lt;meta http-equiv="refresh" content="2"&gt;';
           responseBody += req.session.outputText;
           console.log('***' + JSON.stringify(req.session));
           break;
  
        case SessionState.FINISHED:
           responseBody += 'Execution finished';
           break;
     }
  
     var responseText = '&lt;html&gt;' +
        '&lt;head&gt;' + responseHead + '&lt;/head&gt;' +
        '&lt;body&gt;' + responseBody + '&lt;/body&gt;' +
        '&lt;/html&gt;'
  
     res.send(responseText);
  })
  </code></pre>
  
  <p>The console log shows completely different objects: one is updated by the first function and contains the full output of my program, but the other object (logged by the second function) never changes.</p>
  
  <p>How do I make sure I am always accessing the same session object? If I add req.sessionID to the console output, the value is always the same.</p>