refactor(workspace-client,web): standardize reconnecting WS clients on partysocket

Merged
KI
5mo ago
+684-236

Links

  • Ticket: SUPER-1469
  • Design doc: plans/done/20260712-partysocket-ws-consolidation.md (moved to done in this PR)
  • Motivating incident: #5628

Summary

  • Adds createRelaySocket in @superset/workspace-client: a ~75-line wrapper over partysocket's generic reconnecting WebSocket. It re-signs the URL with a fresh token and runs the _whoowns preflight before every attempt, treats a preflight 403 as a definitive denial (permanent close + onAccessDenied, or slow-poll via accessDeniedRetryMs), and disables buffering so send semantics stay "only while open".
  • Migrates the two simplest hand-rolled reconnect loops onto it with zero consumer changes: the eventBus (desktop host events) and web's WebTerminal/TerminalConnection.
  • The desktop terminal transport is deferred by design (its sleep/wake watchdog, coalescer, and socket-identity logic don't map onto partysocket's silent socket swapping) — rationale in the design doc.

Why / Context

The stale-token reconnect-loop bug (#5628, ~25 users/day) existed because three transports each hand-roll backoff + token freshness and drift independently. partysocket's async URL provider — evaluated before every dial — makes that bug class structurally impossible for anything built on the wrapper. Research/selection rationale in the design doc; partysocket is the maintained reconnecting-websocket fork (Cloudflare/PartyKit, 1.3.0 June 2026).

How It Works

  • The preflight lives inside partysocket's async url provider. Spike result (was the design doc's blocking question): a rejecting provider surfaces as an error event and re-enters partysocket's backoff loop — verified in partysocket's source (_connect's catch → _handleError → _connect) and empirically against a live server.
  • eventBus: connect/scheduleReconnect/disposed machinery deleted; partysocket owns reconnection. fs:watch replay still happens on each open. 403 → 5-min slow poll (unchanged semantics from #5628).
  • Web terminal: keeps the 12-attempt budget (own counter → "error" state, since partysocket's maxRetries exhausts silently), visibility/pageshow/online resume via reconnect() (also the recovery path after budget exhaustion), replay=0 suppression, and fatal exit/error handling. New behavior: web terminals now also get the fly-affinity preflight and fatal-403 handling.
  • Web imports via a new @superset/workspace-client/relay-socket subpath export so the React/tRPC barrel stays out of web's graph.

Manual QA / Verification done

  • Wrapper unit tests (real WS server + scriptable _whoowns): fresh token per attempt, fatal 403 (no dial, single denial callback, CLOSED), 403 slow-poll then recovery when access granted, local URLs skip preflight. Fatal-close test mutation-verified.
  • Live dev desktop (CDP): signed in, sidebar/workspaces load, local /events socket opens through the wrapper (Network.webSocketCreated handshake confirmed), and unreachable relay hosts retry with backoff without console errors (the two React setState warnings present trace to DockBadgeController — pre-existing, unrelated).
  • Not tested live: a real relay-fronted eventBus 403 (needs a second-user setup; covered by the wrapper unit tests and by #5628's relay-side E2E), and web terminal against production relay (typecheck + identical control-flow mapping; worth a spot check on preview).

Testing

  • bun test packages/workspace-client apps/web (17 pass): wrapper behavior (fresh token per attempt, fatal 403, 403 slow-poll recovery, http→ws conversion), eventBus's first-ever regression suite (listener routing, per-host connection sharing, fs:watch refcounting, watch replay after reconnect, last-listener cleanup), and TerminalConnection (dial shape, binary/control delivery, fresh-token + replay=0 redial, permanent stop on exit). The fresh-token redial test fails if #5628's stale-URL bug is reintroduced (mutation-verified).
  • TerminalConnection now takes getToken/relayUrl as constructor deps (WebTerminal injects the real ones) so it's unit-testable without web's env/posthog module graph.
  • bun run lint, bunx turbo typecheck --filter=@superset/desktop --filter=@superset/web --filter=@superset/workspace-client
  • Desktop renderer lib suite: only the 10 pre-existing font-test failures (identical on clean main)

Known Limitations

  • Desktop terminal transport still hand-rolled (deferred; see design doc phase 4).
  • partysocket's connectionTimeout (4s default) now applies to both migrated transports — previously unbounded browser default; strictly tighter.

Open in Stage

Summary by cubic

Standardized reconnecting WebSocket clients on partysocket via createRelaySocket that refreshes tokens, preflights on every attempt, converts http→ws, and handles 403s. Migrated the event bus and web terminal; added regression tests, addressing SUPER-1469.

  • Refactors

    • Added createRelaySocket in @superset/workspace-client: fresh token per dial, _whoowns preflight on each attempt, 403 → permanent close or slow‑poll via accessDeniedRetryMs, http(s)→ws(s) conversion, and unbuffered sends by default (maxEnqueuedMessages=0).
    • Moved event bus and WebTerminal/TerminalConnection to the wrapper: event bus replays fs:watch on open and closes when the last listener unsubscribes; terminal injects getToken/relayUrl, keeps the 12‑attempt budget and resumes via reconnect(), sets replay=0 after first bytes, and stops on exit/error.
    • Recorded the verification outcome in plans/done/20260712-partysocket-ws-consolidation.md.
  • Dependencies

    • Added partysocket@1.3.0; new export @superset/workspace-client/relay-socket; apps/web adds @superset/workspace-client and dev @types/bun (tsconfig includes bun).

Summary by CodeRabbit

  • New Features
    • Introduced a reconnecting relay-socket transport with refreshed authentication for terminal streaming and workspace event delivery.
    • Added public exports for relay-socket creation and related types in the workspace-client package (including a new ./relay-socket entry).
  • Bug Fixes
    • Improved reconnect handling across tab visibility and ensured permanent stop on definitive access denial; prevented retry loops after terminal exit.
  • Tests
    • Added Bun coverage for relay probing/reconnect, terminal reconnect/exit behavior, and event-bus routing + fs watch refcounting.
  • Chores
    • Updated workspace-client dependencies/exports and improved web TypeScript environment typings.