Make session cleanup atomic in leave()

This commit is contained in:
Jared Miller 2026-01-27 17:59:49 -05:00
parent 2de33370cd
commit 8ce1e098e6
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
2 changed files with 5 additions and 8 deletions

View file

@ -1,10 +1,5 @@
import { decode } from "./protocol";
import {
type Client,
getOrCreateSession,
getSession,
removeSession,
} from "./session";
import { type Client, getOrCreateSession, getSession } from "./session";
const PORT = Number(process.env.PORT) || 4040;
@ -66,7 +61,6 @@ Bun.serve({
if (ws.data.room) {
const session = getSession(ws.data.room);
session?.leave(client);
removeSession(ws.data.room);
ws.data.room = null;
}
break;
@ -84,7 +78,6 @@ Bun.serve({
if (ws.data.room && ws.data.client) {
const session = getSession(ws.data.room);
session?.leave(ws.data.client);
removeSession(ws.data.room);
}
console.debug("client disconnected");
},

View file

@ -31,6 +31,10 @@ export class Session {
leave(client: Client) {
this.clients.delete(client);
this.broadcastPeerCount();
if (this.isEmpty()) {
sessions.delete(this.name);
console.debug(`session removed: ${this.name}`);
}
}
isEmpty(): boolean {