Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
A restart costs a blink, not a session: turn, queue and chat survive it and the page reconnects in under a second #453
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/440-a-restart-costs-a-blink-not-a-session-tu → main 1 commit fork
4 files
changed +97
−7
Software/so101/src/restart-session.test.ts added +90
| @@ -0,0 +1,90 @@ | ||
| 1 | import { afterEach, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import WebSocket from "ws"; | |
| 6 | import { FeetechBus } from "./bus.ts"; | |
| 7 | import { stamper } from "./command-gate.ts"; | |
| 8 | import { FakeBus } from "./fake-bus.ts"; | |
| 9 | import { sign } from "./identity.ts"; | |
| 10 | import { Lobby } from "./lobby.ts"; | |
| 11 | import { parseRemote, startRelay, type RunningRelay } from "./relay.ts"; | |
| 12 | import { startServer, type RunningServer } from "./server.ts"; | |
| 13 | ||
| 14 | const KEY = "k".repeat(64); | |
| 15 | const later = Date.now() + 600_000; | |
| 16 | const identity = `id-ann|ann|user|${later}`; | |
| 17 | const headers = { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 18 | const stamp = stamper(); | |
| 19 | const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); | |
| 20 | ||
| 21 | let dir = ""; | |
| 22 | let pi: RunningServer | undefined; | |
| 23 | let relay: RunningRelay | undefined; | |
| 24 | let lobby: Lobby | undefined; | |
| 25 | const sockets: WebSocket[] = []; | |
| 26 | afterEach(async () => { | |
| 27 | for (const s of sockets.splice(0)) s.terminate(); | |
| 28 | await Promise.race([relay?.close(), sleep(500)]); | |
| 29 | lobby?.close(); | |
| 30 | await Promise.race([pi?.close(), sleep(500)]); | |
| 31 | if (dir) rmSync(dir, { recursive: true, force: true }); | |
| 32 | dir = ""; | |
| 33 | relay = lobby = pi = undefined; | |
| 34 | }); | |
| 35 | ||
| 36 | async function boot(port: number) { | |
| 37 | lobby = new Lobby({ identityKey: KEY, adminFile: join(dir, "admin.json"), turnsFile: join(dir, "turns.json"), chatFile: join(dir, "chat.json") }); | |
| 38 | relay = await startRelay({ port, simulation: `http://127.0.0.1:${pi!.port}`, remote: parseRemote({ ARM_SERVER: `ws://127.0.0.1:${pi!.port}` })!, lobby, holdFile: join(dir, "hold.json") }); | |
| 39 | return relay.port; | |
| 40 | } | |
| 41 | ||
| 42 | /** The page's /live socket: resolves with the first state message that names a driver. */ | |
| 43 | function live(port: number, send?: object) { | |
| 44 | const ws = new WebSocket(`ws://127.0.0.1:${port}/live`, { headers }); | |
| 45 | sockets.push(ws); | |
| 46 | const states: any[] = []; | |
| 47 | const state = new Promise<any>((resolve) => | |
| 48 | ws.on("message", (data) => { | |
| 49 | const m = JSON.parse(String(data)); | |
| 50 | if (m.type === "ping") return ws.send(JSON.stringify({ type: "pong" })); | |
| 51 | if (m.type === "state") { states.push(m); if (m.driver || !send) resolve(m); } | |
| 52 | }), | |
| 53 | ); | |
| 54 | ws.on("open", () => send && ws.send(JSON.stringify(send))); | |
| 55 | return { ws, state }; | |
| 56 | } | |
| 57 | ||
| 58 | test("an app restart while someone drives: within 1 s they hold the same turn and a command reaches the arm", async () => { | |
| 59 | dir = mkdtempSync(join(tmpdir(), "so101-restart-session-")); | |
| 60 | const fake = new FakeBus([1, 2, 6]); | |
| 61 | fake.servos.get(1)!.set([0x32, 0x03, 0x9f, 0x0d], 9); // limits 818-3487 | |
| 62 | pi = await startServer(new FeetechBus(fake), { host: "127.0.0.1", port: 0, ids: [1, 2, 6], posesFile: join(dir, "poses.json"), watchEveryMs: 10 }); | |
| 63 | let port = await boot(0); | |
| 64 | const first = live(port, { type: "join" }); | |
| 65 | const before = await first.state; | |
| 66 | expect(before.driver.id).toBe("id-ann"); | |
| 67 | ||
| 68 | first.ws.terminate(); | |
| 69 | await Promise.race([relay!.close(), sleep(500)]); // Bun can keep a server-closed connection open (see relay.test.ts) | |
| 70 | lobby!.close(); | |
| 71 | ||
| 72 | const started = Date.now(); | |
| 73 | port = await boot(port); // the same port: the page reconnects to the same address | |
| 74 | const second = live(port); | |
| 75 | const after = await second.state; | |
| 76 | expect(after.driver.id).toBe("id-ann"); | |
| 77 | expect(after.driver.endsAt).toBe(before.driver.endsAt); | |
| 78 | ||
| 79 | // The driver's next movement works with no extra click: a frame on /follow moves the arm. | |
| 80 | const follow = new WebSocket(`ws://127.0.0.1:${port}/follow`, { headers }); | |
| 81 | sockets.push(follow); | |
| 82 | await new Promise((r, j) => (follow.on("open", r), follow.on("close", () => j(new Error("follow closed"))))); | |
| 83 | const goals = () => fake.writes.filter((w) => w.address === 42 && w.id === 1).map((w) => w.bytes[0]! | (w.bytes[1]! << 8)); | |
| 84 | for (let i = 0; i < 20 && !goals().includes(2100); i++) { | |
| 85 | follow.send(stamp({ 1: 2100 })); | |
| 86 | await sleep(25); | |
| 87 | } | |
| 88 | expect(goals()).toContain(2100); | |
| 89 | expect(Date.now() - started).toBeLessThan(1000); | |
| 90 | }); |
Software/so101/src/ui/lobby-panel.js +2 −2
| @@ -59,8 +59,8 @@ export function whoList(el, m) { | ||
| 59 | 59 | })); |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | /** @param {number} attempt 1 for the first retry @returns {number} ms to wait: 250, 500, 1000, then 2000 at most */ | |
| 63 | export const backoff = (attempt) => Math.min(250 * 2 ** Math.max(0, attempt - 1), 2000); | |
| 62 | /** @param {number} attempt 1 for the first retry @returns {number} ms to wait: 100, 200, 400, 800, 1600, then 2000 at most: a restart costs a blink */ | |
| 63 | export const backoff = (attempt) => Math.min(100 * 2 ** Math.max(0, attempt - 1), 2000); | |
| 64 | 64 | |
| 65 | 65 | /** Tell the page's health meter (src/ui/health-meter.js) what happened. @param {string} what */ |
| 66 | 66 | const tell = (what) => /** @type {any} */ (globalThis).dispatchEvent?.(new Event(what)); |
Software/so101/src/ui/lobby-panel.test.ts +4 −4
| @@ -87,7 +87,7 @@ describe("the room over /live", () => { | ||
| 87 | 87 | } finally { Object.assign(g, saved); } |
| 88 | 88 | }); |
| 89 | 89 | |
| 90 | test("when /live closes: backoff 0.25 to 2 s, a chip until the first message, no motion, no reload", () => { | |
| 90 | test("when /live closes: backoff 0.1 to 2 s, a chip until the first message, no motion, no reload", () => { | |
| 91 | 91 | const sockets: any[] = [], delays: number[] = [], timers: (() => void)[] = []; |
| 92 | 92 | const g = globalThis as any, saved = { WebSocket: g.WebSocket, setInterval: g.setInterval, setTimeout: g.setTimeout, location: g.location }; |
| 93 | 93 | g.WebSocket = class { readyState = 1; sent: string[] = []; constructor() { sockets.push(this); } send(m: string) { this.sent.push(m); } }; |
| @@ -120,13 +120,13 @@ describe("the room over /live", () => { | ||
| 120 | 120 | expect(ws.sent).toEqual([]); // nothing goes while disconnected |
| 121 | 121 | timers.pop()!(); |
| 122 | 122 | } |
| 123 | expect(delays).toEqual([250, 500, 1000, 2000, 2000, 2000]); | |
| 123 | expect(delays).toEqual([100, 200, 400, 800, 1600, 2000]); | |
| 124 | 124 | expect(sockets.length).toBe(7); |
| 125 | 125 | expect(chipOn()).toBe(true); // still waiting: no message yet |
| 126 | 126 | sockets.at(-1).onmessage({ data: JSON.stringify({ type: "hello", signin: false, you: null }) }); |
| 127 | 127 | expect(chipOn()).toBe(false); |
| 128 | 128 | sockets.at(-1).readyState = 3; sockets.at(-1).onclose(); |
| 129 | expect(delays.at(-1)).toBe(250); // a message resets the backoff | |
| 129 | expect(delays.at(-1)).toBe(100); // a message resets the backoff | |
| 130 | 130 | expect(calls).toContain("noStatus"); |
| 131 | 131 | expect(reloads).toEqual([]); |
| 132 | 132 | } finally { Object.assign(g, saved); } |
| @@ -168,7 +168,7 @@ describe("a deliberate reopen", () => { | ||
| 168 | 168 | }); |
| 169 | 169 | |
| 170 | 170 | test("the retry delays", () => { |
| 171 | expect([1, 2, 3, 4, 5, 50].map(backoff)).toEqual([250, 500, 1000, 2000, 2000, 2000]); | |
| 171 | expect([1, 2, 3, 4, 5, 50].map(backoff)).toEqual([100, 200, 400, 800, 1600, 2000]); | |
| 172 | 172 | }); |
| 173 | 173 | |
| 174 | 174 | describe("the chat's scroll", () => { |
Software/so101/src/ui/video.js +1 −1
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | /** No frame for this long and the socket is replaced. */ |
| 10 | 10 | export const QUIET_MS = 2000; |
| 11 | 11 | /** The wait before trying again after a socket that closed or failed, so a relay that is down is not hammered. */ |
| 12 | export const RETRY_MS = 500; | |
| 12 | export const RETRY_MS = 100; | |
| 13 | 13 | |
| 14 | 14 | /** The socket's address on the page's own origin. @param {{ protocol: string, host: string }} loc @param {number | string} id */ |
| 15 | 15 | export function socketUrl(loc, id) { |
new above
offline