Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

fix(cloud): close silent phone connections so the relay can sleep

leepokai92152e898719
diff --git a/cloud/src/relay.ts b/cloud/src/relay.ts
index c2413c28..60a2e8b1 100644
--- a/cloud/src/relay.ts
+++ b/cloud/src/relay.ts
@@ -29,7 +29,8 @@ const HOOK_PER_COMPUTER = 1000;
const HOOK_MAX_BYTES = 16 * 1024 * 1024;
const HOOK_RETRY_MIN_MS = 30_000;
const HOOK_RETRY_MAX_MS = 15 * 60_000;
-const HOST_TIMEOUT_MS = 90_000;
+/** No ping for this long (both sides ping every 30 s): the socket is dead and gets closed. */
+const SILENT_TIMEOUT_MS = 90_000;
const ALARM_MS = 60_000;
/** epk(32) ‖ sig(64) ‖ ciphertext with at least its 16-byte tag (§6.4). */
const MBOX_MIN_BLOB = 32 + 64 + 16;
@@ -567,8 +568,13 @@ export class ComputerRelay extends DurableObject<Env> {
private async closed(ws: WebSocket): Promise<void> {
const a = ws.deserializeAttachment() as Attachment;
this.safeClose(ws, 1000, "");
+ await this.gone(ws, a, Date.now());
+ }
+
+ /** Bookkeeping for a socket that closed or that the DO is closing. */
+ private async gone(ws: WebSocket, a: Attachment, now: number): Promise<void> {
if (a.role === "host") {
- if (a.ready) await this.hostOffline(Date.now(), ws);
+ if (a.ready) await this.hostOffline(now, ws);
} else {
this.rates.delete(a.link);
send(this.readyHost(ws), JSON.stringify({ t: "close", link: a.link }));
@@ -577,14 +583,15 @@ export class ComputerRelay extends DurableObject<Env> {
override async alarm(): Promise<void> {
const now = Date.now();
- for (const ws of this.ctx.getWebSockets("host")) {
- const a = ws.deserializeAttachment() as HostAttachment;
+ // Silent sockets close, device ones too: what the DO sends a peer that stopped reading stays queued,
+ // and the queue keeps the DO awake (billed as duration) until the connection finally dies, often hours.
+ for (const ws of this.ctx.getWebSockets()) {
+ const a = ws.deserializeAttachment() as Attachment;
const last = Math.max(a.at, this.ctx.getWebSocketAutoResponseTimestamp(ws)?.getTime() ?? 0);
- if (now - last > HOST_TIMEOUT_MS) {
- ws.serializeAttachment({ ...a, ready: false });
- this.safeClose(ws, 1011, "timeout");
- if (a.ready) await this.hostOffline(now, ws);
- }
+ if (now - last <= SILENT_TIMEOUT_MS) continue;
+ if (a.role === "host") ws.serializeAttachment({ ...a, ready: false });
+ this.safeClose(ws, 1011, "timeout");
+ await this.gone(ws, a, now);
}
for (const row of this.sql
.exec<{ dk: string; nonce: string }>("SELECT dk, nonce FROM mailbox WHERE exp <= ?", now)
diff --git a/cloud/test/relay.test.ts b/cloud/test/relay.test.ts
index a2d9716e..9aad0e8c 100644
--- a/cloud/test/relay.test.ts
+++ b/cloud/test/relay.test.ts
@@ -228,6 +228,18 @@ describe("presence and forwarding", () => {
await runDurableObjectAlarm(stub(host));
expect(await again.closed()).toBe(1011);
});
+
+ it("closes a silent device socket and tells the host its link is gone", async () => {
+ const { host, hostWs, dev } = await pair();
+ await dev.nextT("presence");
+ const { link } = await hostWs.nextT("open");
+ await runInDurableObject(stub(host), (_, state) => {
+ for (const ws of state.getWebSockets("device")) ws.serializeAttachment({ ...ws.deserializeAttachment(), at: Date.now() - 91_000 });
+ });
+ await runDurableObjectAlarm(stub(host));
+ expect(await dev.closed()).toBe(1011);
+ expect(await hostWs.nextT("close")).toMatchObject({ link });
+ });
});
describe("mailbox", () => {
diff --git a/docs/reference/remote-relay.md b/docs/reference/remote-relay.md
index 2eae4e05..08f48c05 100644
--- a/docs/reference/remote-relay.md
+++ b/docs/reference/remote-relay.md
@@ -424,7 +424,7 @@ Channel 訊息與 DO 訊息的 `t` 值不重疊:channel 只有 `hello`/`welcom
### 7.6 心跳、重連、限流
- 心跳:host 與 device 每 30 秒送 `{"t":"ping"}`(中繼);直連時 host 每 15 秒送 WebSocket Ping frame,client 45 秒沒收到任何資料視為斷線。
-- DO alarm:只要有 socket 或 mailbox 項目就每 60 秒一次:host socket 的 `ctx.getWebSocketAutoResponseTimestamp()` 超過 90 秒 → 以 `1011` 關閉並標記離線;清除過期 mailbox(通知 `mbox.expired`)與過期 nonces。
+- DO alarm:只要有 socket 或 mailbox 項目就每 60 秒一次:任何 socket(host 與 device)的 `ctx.getWebSocketAutoResponseTimestamp()` 超過 90 秒 → 以 `1011` 關閉;host 標記離線,device 通知 host `close` 該 link。device 也要關:送往不再讀取的 peer 的資料會卡在佇列,DO 因此無法 hibernate、整段被計 duration,直到 TCP 斷線(常是數小時)。清除過期 mailbox(通知 `mbox.expired`)與過期 nonces。
- 重連退避:host 1 s 起倍增至 60 s、device 1 s 起倍增至 30 s,皆 full jitter;穩定 60 秒後重置。
- `4100 paired` → 立即以一般模式重連(§4.1)。
- 不重試的 close code:`4001 unauthorized`、`4003 revoked`、`4410 pairingExpired`(device 顯示需重新授權);`4009 replaced` → host 等 5 分鐘再試並記錄 `error`(可能有人複製了 `identity.json`);`4400 unsupportedVersion` → 顯示需要更新。