Skip to content

Uncaught TypeError: Cannot read property 'handleUpgrade' of undefined #442

@Fluffette

Description

@Fluffette

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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions