Skip to content

Commit 170b739

Browse files
committedJan 5, 2021
fix: properly clear timeout on connection failure
Related: #3720
1 parent 230cd19 commit 170b739

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed
 

‎lib/client.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,16 @@ export class Client {
113113
* @private
114114
*/
115115
private doConnect(name: string, auth: object) {
116-
if (this.connectTimeout) {
117-
clearTimeout(this.connectTimeout);
118-
this.connectTimeout = undefined;
119-
}
120116
const nsp = this.server.of(name);
121117

122118
const socket = nsp._add(this, auth, () => {
123119
this.sockets.set(socket.id, socket);
124120
this.nsps.set(nsp.name, socket);
121+
122+
if (this.connectTimeout) {
123+
clearTimeout(this.connectTimeout);
124+
this.connectTimeout = undefined;
125+
}
125126
});
126127
}
127128

@@ -277,5 +278,10 @@ export class Client {
277278
this.conn.removeListener("close", this.onclose);
278279
// @ts-ignore
279280
this.decoder.removeListener("decoded", this.ondecoded);
281+
282+
if (this.connectTimeout) {
283+
clearTimeout(this.connectTimeout);
284+
this.connectTimeout = undefined;
285+
}
280286
}
281287
}

‎test/socket.io.ts

+23
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,29 @@ describe("socket.io", () => {
774774
});
775775
});
776776

777+
it("should close a client without namespace", (done) => {
778+
const srv = createServer();
779+
const sio = new Server(srv, {
780+
connectTimeout: 100,
781+
});
782+
783+
sio.use((_, next) => {
784+
next(new Error("nope"));
785+
});
786+
787+
srv.listen(() => {
788+
const socket = client(srv);
789+
790+
const success = () => {
791+
socket.close();
792+
sio.close();
793+
done();
794+
};
795+
796+
socket.on("disconnect", success);
797+
});
798+
});
799+
777800
describe("dynamic namespaces", () => {
778801
it("should allow connections to dynamic namespaces with a regex", (done) => {
779802
const srv = createServer();

0 commit comments

Comments
 (0)
Please sign in to comment.