fix(desktop): own native WebSocket lifecycle (#2076)

Signed-off-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
This commit is contained in:
Tyler
2026-07-18 14:52:56 -04:00
committed by GitHub
parent 741610f138
commit 4cf5694075
10 changed files with 620 additions and 153 deletions

View File

@@ -1,67 +1,29 @@
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
import { closeWebSocket } from "./relayWebSocketClose.ts";
import { closeAllWebSockets, closeWebSocket } from "./relayWebSocketClose.ts";
test("closeWebSocket sends a Close frame through plugin:websocket|send", async () => {
test("closeWebSocket invokes authoritative native disconnect", async () => {
const calls = [];
await closeWebSocket(42, "community switch", async (cmd, args) => {
calls.push({ cmd, args });
});
assert.equal(calls.length, 1);
assert.equal(calls[0].cmd, "plugin:websocket|send");
assert.deepEqual(calls[0].args, {
id: 42,
message: {
type: "Close",
data: { code: 1000, reason: "community switch" },
},
});
assert.deepEqual(calls, [
{ cmd: "plugin:websocket|disconnect", args: { id: 42 } },
]);
});
test("closeWebSocket swallows send failures (socket already gone)", async () => {
test("closeWebSocket is idempotent when the native socket is gone", async () => {
await closeWebSocket(7, "connection reset", async () => {
throw new Error("WebSocket connection not found");
});
});
// Regression guard: tauri-plugin-websocket registers only `connect` and
// `send` — there is no `disconnect` command. Invoking one rejects silently
// and leaks the socket (relay zombie pile, community-switch disconnects).
// Any socket teardown must go through closeWebSocket.
test("no source file invokes the nonexistent plugin:websocket|disconnect command", () => {
const srcRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"..",
"..",
);
const offenders = [];
const walk = (dir) => {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
walk(full);
continue;
}
if (!/\.(ts|tsx|js|jsx|mjs)$/.test(entry.name)) continue;
if (full === fileURLToPath(import.meta.url)) continue;
if (
fs.readFileSync(full, "utf8").includes("plugin:websocket|disconnect")
) {
offenders.push(path.relative(srcRoot, full));
}
}
};
walk(srcRoot);
assert.deepEqual(
offenders,
[],
"plugin:websocket|disconnect does not exist in tauri-plugin-websocket — use closeWebSocket (Close frame via plugin:websocket|send) instead",
);
test("closeAllWebSockets invokes native process-wide teardown", async () => {
const calls = [];
await closeAllWebSockets(async (cmd, args) => calls.push({ cmd, args }));
assert.deepEqual(calls, [
{ cmd: "plugin:websocket|disconnect_all", args: undefined },
]);
});

View File

@@ -1,28 +1,30 @@
import { invoke } from "@tauri-apps/api/core";
/**
* tauri-plugin-websocket 2.4.2 registers only `connect` and `send` — there is
* no `disconnect` command, so invoking one rejects and the socket leaks. Close
* the way the plugin's own JS API does: send a Close frame; the plugin's read
* loop drops the connection when the peer echoes the Close (or the TCP read
* stream terminates).
* Remove the connection from the native manager before waiting for its socket
* task to stop. The command is bounded and idempotent; failures mean the
* process is already tearing down or the socket is already gone.
*/
export function closeWebSocket(
id: number,
reason: string,
invokeFn: typeof invoke = invoke,
): Promise<void> {
return invokeFn("plugin:websocket|send", {
id,
message: {
type: "Close",
data: { code: 1000, reason },
},
}).then(
return invokeFn("plugin:websocket|disconnect", { id }).then(
() => undefined,
(err) => {
// Expected when the socket is already gone; greppable for anything else.
console.debug(`closeWebSocket(${id}, ${reason}) rejected:`, err);
},
);
}
export function closeAllWebSockets(
invokeFn: typeof invoke = invoke,
): Promise<void> {
return invokeFn("plugin:websocket|disconnect_all").then(
() => undefined,
(err) => {
console.debug("closeAllWebSockets() rejected:", err);
},
);
}