mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Gateway: no orphaned connectors on connect/close races
Message handlers on one WebSocket run concurrently. A connect awaiting the previous connector's teardown could still spawn a new connector after the socket closed, or two connects could both spawn and one got lost. Either way the connector kept running outside MAX_CONNECTIONS and shutdown. Each connect now detaches the previous connection before awaiting it and only spawns if it is still the newest connect on an open socket; the disconnect message and the close handler detach the same way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
8c0d0a3d1d
commit
5f9950a96b
2 files changed
+87
-14
No files matched your search
+26
-14
@@ -708,6 +708,11 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
|
||||
// One browser WebSocket ↔ one Rust connector. Multi-join in the UI opens
|
||||
// multiple /ws connections in parallel (one per server tab).
|
||||
let connection: Ts3Connection | undefined;
|
||||
// Message handlers are async and run concurrently, so a connect can still be
|
||||
// awaiting when a newer connect or the close arrives. Only the newest
|
||||
// connect on an open socket may spawn a connector, else one leaks.
|
||||
let closed = false;
|
||||
let connectGen = 0;
|
||||
|
||||
// Full: accept and say so, since a browser can't read why an upgrade was refused.
|
||||
if (maxConnections() > 0 && wss.clients.size > maxConnections()) {
|
||||
@@ -791,17 +796,16 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
|
||||
}
|
||||
socket.send(JSON.stringify({ type: "turnstilePass", pass: issuePass(ip) }));
|
||||
}
|
||||
// Replacing a connection on the same socket: tear down the previous
|
||||
// connector so we don't leak processes.
|
||||
if (connection) {
|
||||
try {
|
||||
await connection.disconnect();
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
liveConnections.delete(connection);
|
||||
connection = undefined;
|
||||
// Replacing a connection on the same socket: take the previous one
|
||||
// out before awaiting its teardown, so no other handler sees it.
|
||||
const gen = ++connectGen;
|
||||
const prev = connection;
|
||||
connection = undefined;
|
||||
if (prev) {
|
||||
liveConnections.delete(prev);
|
||||
await prev.disconnect().catch(() => {});
|
||||
}
|
||||
if (closed || gen !== connectGen) break;
|
||||
const options: Ts3ConnectOptions = {
|
||||
host: msg.host,
|
||||
nickname: msg.nickname,
|
||||
@@ -1095,9 +1099,13 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
|
||||
break;
|
||||
}
|
||||
case "disconnect": {
|
||||
await connection?.disconnect(msg.message ?? "");
|
||||
if (connection) liveConnections.delete(connection);
|
||||
// Same as above: detach first, a connect may arrive while this awaits.
|
||||
const current = connection;
|
||||
connection = undefined;
|
||||
if (current) {
|
||||
liveConnections.delete(current);
|
||||
await current.disconnect(msg.message ?? "");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -1113,7 +1121,11 @@ wss.on("connection", (socket: WebSocket, req: IncomingMessage) => {
|
||||
if (remaining > 0) connectionsPerIp.set(ip, remaining);
|
||||
else connectionsPerIp.delete(ip);
|
||||
heartbeats.delete(socket);
|
||||
if (connection) liveConnections.delete(connection);
|
||||
connection?.disconnect();
|
||||
closed = true;
|
||||
if (connection) {
|
||||
liveConnections.delete(connection);
|
||||
void connection.disconnect().catch(() => {});
|
||||
connection = undefined;
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user