engine.io icon indicating copy to clipboard operation
engine.io copied to clipboard

Uncaught TypeError: Cannot read property 'handleUpgrade' of undefined

Open Fluffette opened this issue 10 years ago • 3 comments

After upgrading socket.io to version 1.5.1, I got the following error (it didn't occur with socket.io 1.5.0):

Uncaught TypeError: Cannot read property 'handleUpgrade' of undefined
      at node_modules\engine.io\lib\server.js:343:12
      at Server.verify (node_modules\engine.io\lib\server.js:162:3)
      at Server.handleUpgrade (node_modules\engine.io\lib\server.js:332:8)
      at Server.<anonymous> (node_modules\engine.io\lib\server.js:453:14)
      at onParserExecuteCommon (_http_server.js:409:14)
      at HTTPParser.onParserExecute (_http_server.js:377:5)

The error comes from engine.io, whose version has been updated to 1.7.2 through the update of socket.io to 1.5.1. I searched in the code of server.js, and found that the guilty line is:

self.ws.handleUpgrade(req, socket, head, function(conn){
    self.onWebSocket(req, conn);
});

The field ws is instantiated in the constructor of the Server object (server.js line 75):

this.ws = new WebSocketServer({
    noServer: true,
    clientTracking: false,
    perMessageDeflate: this.perMessageDeflate,
    maxPayload: this.maxHttpBufferSize
});

But when you close the server with the close() function, we see in the code of server.js (line 182 to 195) that the field ws is deleted:

if (this.ws) {
    debug('closing webSocketServer');
    this.ws.close();
    delete this.ws;
}

And this was not the case in previous version of engine.io. This field is not instantiated again when you call the listen() function later on your server. That's why the error occur, ws is undefined.

Here is a simple piece of code reproducing the error:

var ioClient = require('socket.io-client');
var express = require('express');
var http = express();
var server = require('http').Server(http);
var io = require('socket.io')(server);

io.on('connection', function (socket) {
	console.log('connection :', socket.id);
});

server.listen(80, function () {
	// do some stuff ...
	server.close(); // ws is deleted
	server.listen(80, function () {
		var socket = ioClient.connect('http://localhost');
		// the socket will trigger a call to handleUpgrade() function
		// and the error will occur
	});
});

Try to run this code with a previous version of engine.io (through socket.io 1.5.0 eg) and the error will not occur. With the latest version of engine.io (through socket.io 1.5.1), the error will occur.

Is this a real issue, or am I missing something ?

Fluffette avatar Nov 03 '16 15:11 Fluffette

It does indeed look like a bug (the change itself: https://github.com/socketio/engine.io/pull/393)

I guess the line delete this.ws; should be removed, since this.ws can be reused later.

darrachequesne avatar Nov 03 '16 23:11 darrachequesne

+1 Got the same problem in my tests where I check for socket reconnection

perrin4869 avatar Nov 05 '16 06:11 perrin4869

I had used engine.io version 2 and issue still exists but down graded to engine.io and it was OK!

FarzanRNobakht avatar Jan 27 '17 13:01 FarzanRNobakht