Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up namespaces after client is rejected in middleware #4773

Merged
merged 3 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,6 @@ export class Socket<
}

this._cleanup();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_cleanup is called here and in two more places: https://github.com/socketio/socket.io/blob/main/lib/namespace.ts#L327-L333

I believe the this.nsp._remove(this); should be called in all these cases

this.nsp._remove(this);
this.client._remove(this);
this.connected = false;
this.emitReserved("disconnect", reason, description);
return;
}
Expand All @@ -772,6 +769,9 @@ export class Socket<
*/
_cleanup() {
this.leaveAll();
this.nsp._remove(this);
this.client._remove(this);
this.connected = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this.client._remove(this); and this.connected = false; are needed here, since the socket is not actually connected if it's rejected by the middleware. What do you think?

Copy link
Contributor Author

@carera carera Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I just thought it does no harm since client._remove has a guard check and no-ops if the socket is not actually connected, but yeah, let me move it out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

this.join = noop;
}

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions test/namespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,76 @@ describe("namespaces", () => {
io.of(/^\/dynamic-\d+$/);
});

it("should NOT clean up namespace when cleanupEmptyChildNamespaces is OFF and client is rejected in middleware", (done) => {
const io = new Server(0, { cleanupEmptyChildNamespaces: false });
io.of(/^\/dynamic-\d+$/).use((socket, next) => {
next(new Error("You shall not pass!"));
});
const c1 = createClient(io, "/dynamic-101");

c1.on("connect", () => {
done(new Error("Should not connect"));
});

c1.on("connect_error", () => {
setTimeout(() => {
expect(io._nsps.has("/dynamic-101")).to.be(true);
expect(io._nsps.get("/dynamic-101")!.sockets.size).to.be(0);
success(done, io, c1);
}, 100);
});
});

it("should clean up namespace when cleanupEmptyChildNamespaces is ON and client is rejected in middleware", (done) => {
const io = new Server(0, { cleanupEmptyChildNamespaces: true });
io.of(/^\/dynamic-\d+$/).use((socket, next) => {
next(new Error("You shall not pass!"));
});
const c1 = createClient(io, "/dynamic-101");

c1.on("connect", () => {
done(new Error("Should not connect"));
});

c1.on("connect_error", () => {
setTimeout(() => {
expect(io._nsps.has("/dynamic-101")).to.be(false);
success(done, io, c1);
}, 100);
});
});

it("should NOT clean up namespace when cleanupEmptyChildNamespaces is ON and client is rejected in middleware but there are other clients connected", (done) => {
const io = new Server(0, { cleanupEmptyChildNamespaces: true });
let clientIdxToReject = 0;
io.of(/^\/dynamic-\d+$/).use((socket, next) => {
if (clientIdxToReject) {
next(new Error("You shall not pass!"));
} else {
next();
}
clientIdxToReject++;
});
const c1 = createClient(io, "/dynamic-101");

c1.on("connect", () => {
const c2 = createClient(io, "/dynamic-101");
c2.on("connect", () => {
done(new Error("Client 2 should not connect"));
});
c2.on("connect_error", () => {
setTimeout(() => {
expect(io._nsps.has("/dynamic-101")).to.be(true);
expect(io._nsps.get("/dynamic-101")!.sockets.size).to.be(1);
success(done, io, c1, c2);
}, 100);
});
});
c1.on("connect_error", () => {
done(new Error("Client 1 should not get an error"));
});
});

it("should attach a child namespace to its parent upon manual creation", () => {
const io = new Server(0);
const parentNamespace = io.of(/^\/dynamic-\d+$/);
Expand Down