Skip to content

fix(broker): keep a silent hub from wedging the native broker - #301

Merged
eliahilse merged 1 commit into
mainfrom
fix/broker-hang-deadlock
Nov 25, 2026
Merged

eliahilse merged 1 commit into
mainfrom
fix/broker-hang-deadlock

Conversation

@eliahilse

Copy link
Copy Markdown
Member

TL;DR

Summary

  • A hub that stops answering on an open socket no longer wedges the native broker. hibernate and machine_wake used to wait for the hub's reply while holding the core lock, and nothing bounded that wait. Every later request queued behind them forever, and SIGTERM was ignored too.
  • hibernate and machine_wake now leave their hub work as a HubJob, which Core::execute runs after it releases the core. The hibernate mirror keeps the TypeScript broker's 10 s barrier, and the wakers are still kicked after the push.
  • Every hub request now gives up after 15 s, and its waiter is removed even when the caller drops the request. A socket that stays silent that long is hung up and linked again. forward still waits under the core, but every attempt and every backoff now fits inside forward_wait.
  • Shutdown stops the bridge without taking the core, which releases any pending hub request. It waits at most 1 s for the core, and the chores and the connection drain share one 3 s deadline, so daemon stop finishes inside its 5 s window.
  • A watchdog logs when the core has been held for more than 30 s, and names the request that took it last.
  • The TypeScript broker gets the same 15 s reply limit (replyTimeoutMs) on its hub requests. It never had the wedge, because it has no global lock, but a silent socket was never torn down either.

Root cause

Bridge::request (crates/scp-broker/src/bridge/link.rs) awaited the hub's reply on a oneshot with no timeout. The waiter only failed once the socket closed. Core::hibernate_rpc → bridge_mirror → mirror_now (hibernate.rs:287) and Core::machine_wake (bridge/mod.rs:241) awaited it while holding the core lock taken in serve_connection. Picture a hub that answers the bridge's hello and then goes quiet on a live socket, for example while it is overloaded or flapping. A hibernate from the periodic claude-bg task then holds the core forever. The claude-bg task itself hangs, and the broker kills it after 600 s, but the server-side request keeps the lock. Broker::run began shutdown with self.core.lock().await, so SIGTERM hung behind the same lock.

The hub flapping triggered the hang. The unbounded await under the lock was the cause.

Test plan

  • crates/scp-broker/tests/bridge_stall.rs uses a fake hub that links and then goes silent. On main, both tests fail: the broker stops answering while a hibernate waits on the hub, and SIGTERM does not stop it while a machine_wake waits. With this change, both pass.
  • server::tests::shutdown_finishes_while_a_request_holds_the_core: shutdown finishes while a stalled store write holds the core.
  • packages/broker/src/bridge-silent.test.ts: a request the silent hub never answers fails in time, and the socket is linked again. It times out on main.
  • cargo fmt --check, cargo clippy --workspace --all-targets -D warnings, cargo test --workspace
  • Broker conformance on ts, rust and ts-native with SCP_BROKER_WAKERS=1
  • CLI suites with native routing, plus conformance cli and lifecycle with native routing
  • bunx biome ci ., bun run lint, bun run typecheck, bun scripts/relay-version.ts --check, bun scripts/native-changeset.ts --base origin/main

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Co-Authored-By: GPT 6.1 Sol codex@openai.com

hibernate and machine_wake awaited the hub's reply under the core lock
with no time limit, so a hub that went quiet on an open socket blocked
every request and SIGTERM. Hub work now runs after the core is released,
every hub request is bounded at 15 s and hangs up a silent socket,
shutdown no longer waits on a busy core, and a watchdog logs a core held
past 30 s. The TypeScript bridge gets the same reply limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: GPT 6.1 Sol <codex@openai.com>
@eliahilse
eliahilse merged commit a22ea73 into main Nov 25, 2026
16 checks passed
@eliahilse
eliahilse deleted the fix/broker-hang-deadlock branch November 25, 2026 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant