Skip to content

Commit 46213a6

Browse files
fix: prevent duplicate connections when multiplexing
This bug was introduced in [1]: a multiplexed socket could in some cases send multiple CONNECT packets, resulting in duplicate connections on the server side. A cached socket will now be reopened only if it was inactive, that is, if one had explicitly called socket.disconnect() before. Related: #1460 [1]: b7dd891
1 parent 4996f9e commit 46213a6

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

Diff for: lib/manager.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,7 @@ export class Manager<
465465
if (!socket) {
466466
socket = new Socket(this, nsp, opts);
467467
this.nsps[nsp] = socket;
468-
}
469-
470-
if (this._autoConnect) {
468+
} else if (this._autoConnect && !socket.active) {
471469
socket.connect();
472470
}
473471

Diff for: test/connection.ts

+53
Original file line numberDiff line numberDiff line change
@@ -838,4 +838,57 @@ describe("connection", () => {
838838
});
839839
});
840840
});
841+
842+
it("should not reopen a cached but active socket", () => {
843+
return wrap((done) => {
844+
const manager = new Manager(BASE_URL, {
845+
autoConnect: true,
846+
});
847+
848+
let i = 0;
849+
const expected = ["0", "1"];
850+
851+
manager.engine.on("packetCreate", ({ data }) => {
852+
expect(data).to.eql(expected[i++]);
853+
});
854+
855+
manager.once("open", () => {
856+
const socket = manager.socket("/");
857+
const socket2 = manager.socket("/");
858+
859+
expect(socket2 === socket).to.be(true);
860+
861+
socket.on("connect", () => {
862+
socket.disconnect();
863+
done();
864+
});
865+
});
866+
});
867+
});
868+
869+
it("should not reopen an already active socket", () => {
870+
return wrap((done) => {
871+
const manager = new Manager(BASE_URL, {
872+
autoConnect: true,
873+
});
874+
875+
let i = 0;
876+
const expected = ["0", "0/foo,", "1", "1/foo,"];
877+
878+
manager.engine.on("packetCreate", ({ data }) => {
879+
expect(data).to.eql(expected[i++]);
880+
});
881+
882+
manager.once("open", () => {
883+
const socket = manager.socket("/");
884+
const socketFoo = manager.socket("/foo");
885+
886+
socket.on("connect", () => {
887+
socket.disconnect();
888+
socketFoo.disconnect();
889+
done();
890+
});
891+
});
892+
});
893+
});
841894
});

0 commit comments

Comments
 (0)