diff --git a/lib/uws.ts b/lib/uws.ts index 1abc6a0184..6e400c7f77 100644 --- a/lib/uws.ts +++ b/lib/uws.ts @@ -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); + } }); } }; diff --git a/test/uws.ts b/test/uws.ts index de185d7c03..814a2467fd 100644 --- a/test/uws.ts +++ b/test/uws.ts @@ -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;