Skip to content

Commit

Permalink
fix(uws): prevent the server from crashing after upgrade
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed Sep 2, 2022
1 parent 2803871 commit ba497ee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/uws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export function patchAdapter(app /* : TemplatedApp */) {
if (isNew) {
socket.conn.on("upgrade", () => {
const rooms = this.sids.get(id);
subscribe(this.nsp.name, socket, isNew, rooms);
if (rooms) {
subscribe(this.nsp.name, socket, isNew, rooms);
}
});
}
};
Expand Down
6 changes: 6 additions & 0 deletions test/uws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ describe("socket.io with uWebSocket.js-based engine", () => {
io.to("room1").emit("hello");
});

it("should not crash when socket is disconnected before the upgrade", (done) => {
client.on("disconnect", () => done());

io.of("/").sockets.get(client.id)!.disconnect();
});

it("should serve static files", (done) => {
const clientVersion = require("socket.io-client/package.json").version;

Expand Down

0 comments on commit ba497ee

Please sign in to comment.