Session: TypeError: Cannot read property 'length' of null

Created on 5 May 2017  路  6Comments  路  Source: expressjs/session

I am getting this issue in session when i travel from one specific page to other, what can be the reason behind it

_http_outgoing.js:138

      this.outputSize += this._header.length;
                                         ^

TypeError: Cannot read property 'length' of null
at ServerResponse.OutgoingMessage._send (_http_outgoing.js:138:38)
at ServerResponse.OutgoingMessage.write (_http_outgoing.js:492:16)
at ServerResponse.OutgoingMessage.end (_http_outgoing.js:589:10)
at writeend (/home/...../my_app/node_modules/express-session/index.js:261:22)
at Immediate.onsave (/home/...../my_app/node_modules/express-session/index.js:335:11)
at runCallback (timers.js:666:20)
at tryOnImmediate (timers.js:639:5)
at processImmediate [as _immediateCallback] (timers.js:611:5)

awaiting more info

Most helpful comment

Hi @dougwilson ,

I found the issue its due to the 404 error handling i wrote which messed up with WebSocket GET handshake

router.get('*', function(req, res) {
  res.status(404).send('Page NOT Found -_-\'');
}); 

Thanks and Regards.

All 6 comments

Hi @mohanen I'm not 100% sure if that is an issue with this module or not. I can dig in, but need information to reproduce the issue. If you're not sure where to start, please provide the following:

  1. The exact version of this module
  2. The exact version of Node.js
  3. The exact versions of all other modules needs to reproduce.
  4. Full, complete source code we can copy and paste into a file and run.
  5. Full, complete instructions on how to reproduce. Please leave nothing implied, as everything you just leave to implied makes it that much more unlikely for us to reproduce. For example, saying "make a request to the server" is not enough information. What does the cookie jar in the browser contain when the request is made, for example?

Hi @dougwilson ,

I found the issue its due to the 404 error handling i wrote which messed up with WebSocket GET handshake

router.get('*', function(req, res) {
  res.status(404).send('Page NOT Found -_-\'');
}); 

Thanks and Regards.

Hi,

I occasionally get this exact error and I also have WebSockets in my application.
But why does that code cause this issue? And what did you change to make it work properly>

As far as my understanding, if you have a web socket in a path say

www.somesite.com/ws_sample:3000

You connect to it with

ws://www.somesite.com/ws_sample:3000

basically web sockets are upgraded http requests , so a http request is made to www.somesite.com/ws_sample:3000

In this case if you have a 404 error handlers it messes up with this request since that path "ws_sample" has not defined for http response .

Try removing any error handlers like app.get('*',....
or if you have any middle ware app.use(ThirdParty404ErrorHandler)

Oh thank you, I think that I have found it.

I have the following error handler at the bottom of my app file

app.use(function (err, req, res) {
    log.error('Error on path %s\n%s\n', req.url, err.stack);
    res.status(500).send((process.env.NODE_ENV == 'production') ? 'Internal Server Error' : err.stack.replace(/(?:\r\n|\r|\n)/g, '<br />'));
});

And for some reason I never noticed the following line in my log, just before it crashes:

2017-07-12T11:19:57.796Z - error: Error on path /players/.websocket?id={{the_id}}

My guess is that ever so often the handshake fails and that this is when the error handler is invoked.

@Vinno97 provided some valuable information over in the Express repo. If you're encountering this issue and using the express-ws module, you can follow the issue in the tracker at https://github.com/HenningM/express-ws/issues/64

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fibo picture fibo  路  22Comments

scaryguy picture scaryguy  路  16Comments

renehauck picture renehauck  路  16Comments

gk0us picture gk0us  路  18Comments

alex55132 picture alex55132  路  22Comments