Skip to content

Commit

Permalink
fix: properly clear timeout on connection failure
Browse files Browse the repository at this point in the history
Related: #3720
  • Loading branch information
darrachequesne committed Jan 5, 2021
1 parent 230cd19 commit 170b739
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,16 @@ export class Client {
* @private
*/
private doConnect(name: string, auth: object) {
if (this.connectTimeout) {
clearTimeout(this.connectTimeout);
this.connectTimeout = undefined;
}
const nsp = this.server.of(name);

const socket = nsp._add(this, auth, () => {
this.sockets.set(socket.id, socket);
this.nsps.set(nsp.name, socket);

if (this.connectTimeout) {
clearTimeout(this.connectTimeout);
this.connectTimeout = undefined;
}
});
}

Expand Down Expand Up @@ -277,5 +278,10 @@ export class Client {
this.conn.removeListener("close", this.onclose);
// @ts-ignore
this.decoder.removeListener("decoded", this.ondecoded);

if (this.connectTimeout) {
clearTimeout(this.connectTimeout);
this.connectTimeout = undefined;
}
}
}
23 changes: 23 additions & 0 deletions test/socket.io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,29 @@ describe("socket.io", () => {
});
});

it("should close a client without namespace", (done) => {
const srv = createServer();
const sio = new Server(srv, {
connectTimeout: 100,
});

sio.use((_, next) => {
next(new Error("nope"));
});

srv.listen(() => {
const socket = client(srv);

const success = () => {
socket.close();
sio.close();
done();
};

socket.on("disconnect", success);
});
});

describe("dynamic namespaces", () => {
it("should allow connections to dynamic namespaces with a regex", (done) => {
const srv = createServer();
Expand Down

0 comments on commit 170b739

Please sign in to comment.