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

Master: req.wsclient looks null occasionally

Open crickeys opened this issue 14 years ago • 14 comments

Hey Guille,

Was excited to see if the websocket memory leaks went away seeing that you switched over to WS. When running the code from the master branch I'm getting a lot of these errors. Looks like req.wsclient is coming in null. Any ideas? I'm running node v0.8.8. Could an old socket.io-client file being cached in the browser cause this? I am serving that file independently of socket.io.

6 Sep 18:59:25 - Uncaught exception: TypeError: Cannot call method 'send' of undefined
data:    TypeError: Cannot call method 'send' of undefined
data:        at WebSocket.write (/home/node/node_modules/socket.io/lib/transports/websocket.js:65:17)
data:        at WebSocket.Transport.packet (/home/node/node_modules/socket.io/lib/transport.js:515:15)
data:        at WebSocket.Transport.heartbeat (/home/node/node_modules/socket.io/lib/transport.js:330:10)
data:        at Object._onTimeout (/home/node/node_modules/socket.io/lib/transport.js:301:12)
data:        at Timer.list.ontimeout (timers.js:101:19)

6 Sep 18:59:25 - Uncaught exception: TypeError: Cannot set property 'onerror' of undefined
data:    TypeError: Cannot set property 'onerror' of undefined
data:        at new WebSocket (/home/node/node_modules/socket.io/lib/transports/websocket.js:33:24)
data:        at Manager.handleClient (/home/node/node_modules/socket.io/lib/manager.js:674:19)
data:        at Manager.handleHTTPRequest (/home/node/node_modules/socket.io/lib/manager.js:642:8)
data:        at Manager.handleRequest (/home/node/node_modules/socket.io/lib/manager.js:601:12)
data:        at Server.<anonymous> (/home/node/node_modules/socket.io/lib/manager.js:119:10)
data:        at Server.EventEmitter.emit (events.js:115:20)
data:        at HTTPParser.parser.onIncoming (http.js:1793:12)
data:        at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)
data:        at Socket.socket.ondata (http.js:1690:22)
data:        at TCP.onread (net.js:402:27)

ALSO, kudos on the book! Reading it now :)

crickeys avatar Sep 06 '12 19:09 crickeys

@guille Any idea on this? Where does req.wsclient get set originally?

crickeys avatar Sep 13 '12 19:09 crickeys

https://github.com/LearnBoost/socket.io/blob/master/lib/manager.js#L630

rauchg avatar Sep 13 '12 21:09 rauchg

For some reason https://github.com/LearnBoost/socket.io/blob/master/lib/manager.js#L675 sometimes gets called before https://github.com/LearnBoost/socket.io/blob/master/lib/manager.js#L630 get's called. And then we have a websocket consctructor that get's called on something that doesn't have a wsclient so it all breaks. As though some browser sent an http request that was considered a websocket request but before handleUpgrade is ever called.

Any ideas?

crickeys avatar Sep 16 '12 08:09 crickeys

OH! I know what it is (I think)

rauchg avatar Sep 16 '12 18:09 rauchg

[crosses fingers]

crickeys avatar Sep 16 '12 18:09 crickeys

Experiencing the same error with socket.io from git

../node_modules/socket.io/lib/transports/websocket.js:33 req.wsclient.onerror = function(){ ^ TypeError: Cannot set property 'onerror' of undefined at new WebSocket (../node_modules/socket.io/lib/transports/websocket.js:33:24) at Manager.handleClient (../node_modules/socket.io/lib/manager.js:661:19) at Manager.handleUpgrade (../node_modules/socket.io/lib/manager.js:618:8) at HTTPServer. (../node_modules/socket.io/lib/manager.js:122:10) at HTTPServer.EventEmitter.emit (events.js:115:20) at Socket.socket.ondata (http.js:1710:14) at TCP.onread (net.js:402:27)

@guille Do you believe a fix is possible??

Best

ghost avatar Sep 19 '12 14:09 ghost

I currently fixed it on my deployment adding an if statement to transports/websocket.js

33 if ( typeof ( req.wsclient ) != 'undefined' ) { 34 req.wsclient.onerror = function(){ 35 self.end('socket error'); 36 }; 37 req.wsclient.onclose = function(){ 38 self.end('socket end'); 39 }; 40 req.wsclient.onmessage = function(ev){ 41 self.onMessage(parser.decodePacket(ev.data)); 42 }; 43 }

This causes no more errors - however might it lead to a memory leak?

Best

ghost avatar Sep 20 '12 09:09 ghost

@guille said he thought he knew the issue. I think checking for undefined or null or whatever doesn't solve the main issue and I would be worried that some clients are not connecting correctly. I think you'll also need to add a check for != null with the prototype.write and protoype.read methods as well. Again, I think there's a different issue at work. Any ideas @guille?

crickeys avatar Sep 20 '12 18:09 crickeys

Just to note I'm also seeing a lot of this exception working with current master. Boo!

cantlin avatar Sep 25 '12 10:09 cantlin

Just wondering if there is any status on this we are using seeing a lot of these issues also

zachaller avatar Oct 01 '12 18:10 zachaller

@guille any update on this?

lpinca avatar Oct 08 '12 13:10 lpinca

from looks of it, previously upgrade was completely blocking call, but now with usage of ws lib it has became asynchronous which allows client to issue new requests while upgrade hasn't been finished yet

dainis avatar Oct 23 '12 06:10 dainis

Does version 9.13 remove the use of ws lib? Something seems to have changed as I haven't seen this error occur again since my most recent deploy (going for at least a week now).

CygnusX avatar Jan 05 '13 16:01 CygnusX

From looking around, socket.io may execute handleClient before the upgrade happens. The upgrade gives the request object its wsclient. So I've modified the constructor to check if we have a wsclient yet, if not, we end the transport, which closes the connection, triggering a reconnect. Please see this patch.

https://github.com/hoangfb/socket.io/commit/70b90166fc16f1bd824bef4d4a9dbc56b9343ea7 https://github.com/hoangfb/socket.io/commit/7ee870405b5bad4b85a3bd3421d53ed7429a15ae

Thoughts?

hoangfb avatar Jan 23 '13 16:01 hoangfb