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

fix: fix race conditions with dynamic namespaces (#4136) #4137

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,15 @@ export class Client<
(
dynamicNspName:
| Namespace<ListenEvents, EmitEvents, ServerSideEvents>
| false
| false,
race: boolean = false
) => {
if (dynamicNspName) {
debug("dynamic namespace %s was created", dynamicNspName);
if (race) {
debug("dynamic namespace %s already created", dynamicNspName);
} else {
debug("dynamic namespace %s was created", dynamicNspName);
}
this.doConnect(name, auth);
} else {
debug("creation of namespace %s was denied", name);
Expand Down
7 changes: 6 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ export class Server<
name: string,
auth: { [key: string]: any },
fn: (
nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents> | false
nsp: Namespace<ListenEvents, EmitEvents, ServerSideEvents> | false,
race?: boolean
) => void
): void {
if (this.parentNsps.size === 0) return fn(false);
Expand All @@ -203,6 +204,10 @@ export class Server<
nextFn.value(name, auth, (err, allow) => {
if (err || !allow) {
run();
} else if (this._nsps.has(name)) {
// See #4136. It's possible that in the meantime the namespace has
// already been created, so we'll have to handle this properly.
fn(this._nsps.get(name) as Namespace, true);
} else {
const namespace = this.parentNsps
.get(nextFn.value)!
Expand Down
42 changes: 42 additions & 0 deletions test/socket.io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { io as ioc, Socket as ClientSocket } from "socket.io-client";
import "./support/util";
import "./utility-methods";

type callback = (err: Error | null, success: boolean) => void;

// Creates a socket.io client for the given server
function client(srv, nsp?: string | object, opts?: object): ClientSocket {
if ("object" == typeof nsp) {
Expand Down Expand Up @@ -998,6 +1000,46 @@ describe("socket.io", () => {
const socket = client(srv, "/dynamic-101");
});
});

it("should handle race conditions with dynamic namespaces (#4136)", (done) => {
const srv = createServer();
const sio = new Server(srv);
const counters = {
connected: 0,
created: 0,
events: 0,
};
const buffer: callback[] = [];
sio.on("new_namespace", (namespace) => {
counters.created++;
});
srv.listen(() => {
const handler = () => {
if (++counters.events === 2) {
expect(counters.created).to.equal(1);
done();
}
};

sio
.of((name, query, next) => {
buffer.push(next);
if (buffer.length === 2) {
buffer.forEach((next) => next(null, true));
}
})
.on("connection", (socket) => {
if (++counters.connected === 2) {
sio.of("/dynamic-101").emit("message");
}
});

let one = client(srv, "/dynamic-101");
let two = client(srv, "/dynamic-101");
one.on("message", handler);
two.on("message", handler);
});
});
});
});

Expand Down