Skip to content

Commit ba497ee

Browse files
committedSep 2, 2022
fix(uws): prevent the server from crashing after upgrade
This should fix a rare case where the Engine.IO connection was upgraded to WebSocket while the Socket.IO socket was disconnected, which would result in the following exception: > TypeError: Cannot read properties of undefined (reading 'forEach') > at subscribe (/node_modules/socket.io/dist/uws.js:87:11) > at Socket.<anonymous> (/node_modules/socket.io/dist/uws.js:28:17) > at Socket.emit (node:events:402:35) > at WebSocket.onPacket (/node_modules/engine.io/build/socket.js:214:22) > at WebSocket.emit (node:events:390:28) > at WebSocket.onPacket (/node_modules/engine.io/build/transport.js:92:14) > at WebSocket.onData (/node_modules/engine.io/build/transport.js:101:14) > at message (/node_modules/engine.io/build/userver.js:56:30) Related: #4443
1 parent 2803871 commit ba497ee

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed
 

‎lib/uws.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export function patchAdapter(app /* : TemplatedApp */) {
2525
if (isNew) {
2626
socket.conn.on("upgrade", () => {
2727
const rooms = this.sids.get(id);
28-
subscribe(this.nsp.name, socket, isNew, rooms);
28+
if (rooms) {
29+
subscribe(this.nsp.name, socket, isNew, rooms);
30+
}
2931
});
3032
}
3133
};

‎test/uws.ts

+6
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ describe("socket.io with uWebSocket.js-based engine", () => {
186186
io.to("room1").emit("hello");
187187
});
188188

189+
it("should not crash when socket is disconnected before the upgrade", (done) => {
190+
client.on("disconnect", () => done());
191+
192+
io.of("/").sockets.get(client.id)!.disconnect();
193+
});
194+
189195
it("should serve static files", (done) => {
190196
const clientVersion = require("socket.io-client/package.json").version;
191197

0 commit comments

Comments
 (0)
Please sign in to comment.