Skip to content

fix(relay): the demand wake signal must not fail the grant it wakes for - #20064

Open
nwparker wants to merge 4 commits into
fix/18211-lan-mode-applies-to-paired-devicesfrom
nwparker/relay-transient-demand-wake
Open

nwparker wants to merge 4 commits into
fix/18211-lan-mode-applies-to-paired-devicesfrom
nwparker/relay-transient-demand-wake

Conversation

@nwparker

@nwparker nwparker commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​119 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​119
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​23 $\color{#cf222e}{\Huge{\mathbf{−}}}$​4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​19

Stacked on #19870 (fix/18211-lan-mode-applies-to-paired-devices). Review the top commit only.

What breaks

withTransientDemand called refreshDemand() bare on both sides of the operation it wraps. That call is not pure — it reaches the device registry (nextPendingExpiry → listDevices) and the settings store (hasDemand → isRelayAllowedForDevice), so it can fail on its own.

Measured, each call site failed the operation instead:

The pre-call threw with the transient ref already acquired. The ref was never released and the operation never ran. Transient refs have no expiry, so that ref holds relay demand for the rest of the process — the relay stays online forever because a wake signal failed.

The teardown call replaced the operation's own result, in both directions:

  • a named mint failure arrived as listDevices exploded
  • a successful mint arrived as a rejection

Why containment is the right shape

Both calls are wake signals. The liveness tick and the next refresh re-ask, so a lost signal is recoverable — where a stranded transient ref is not, and a successful mint reported as a failure is not. Containing the signal trades a recoverable loss for two unrecoverable ones.

The containment lives beside the ledger that owns the ref, not in desktop-relay-service.ts — that file is at its max-lines ceiling and this must not cost it a line. (A max-lines disable is not an option in this repo.)

Also in here

The mid-operation policy-flip rewrite was discarding the original error. It now carries it as cause — the one place in a commit about naming causes that was destroying one.

Base

main does not work — this touches desktop-relay-service.ts and relay-demand-ledger.ts as they stand after #19870's stack. Merge order: #19870 → this.

Verification

tsc --noEmit -p config/tsconfig.node.json clean · src/main/runtime/relay — 25 files / 204 tests passed, 0 failures · oxlint clean · oxfmt --check clean.

Cherry-picked from nwparker/adv3-failure-fixes, which has no PR.

Correction to the analysis above (2026-09-16)

The claim that hasDemand → isRelayAllowedForDevice can fail the caller is wrong, and the fix is narrower than the description implies. coordinator.reconcile() → beginReconcile → reconcileEpoch is an async function whose whole body — including readContext() and hasDemand?.(context) — runs inside its own try/catch, so a throw there becomes a caught rejection logged as [relay] broker reconcile failed:. It cannot escape refreshDemand.

The only synchronous throw surface that can is nextPendingExpiry() → listDevices(), and it runs after the coordinator has already been woken. So what a contained throw actually costs is the demandExpiryTimer re-arm, not the wake.

That also narrows the "recoverable" argument: ensureLive() returns early when ownership is valid and live and never touches the expiry timer, so an unscanned invite's expiry cannot drop standing demand until some later refreshDemand. The containment is still correct and worth having — this note only corrects why.

Pre-existing, not introduced by this PR: the same throw point behaved this way before.

Rebase record (2026-09-11)

Stack preserved: base stays fix/18211-lan-mode-applies-to-paired-devices (#19870), rebased onto main at 5fa62feda7c264c6eda99ba4788ffebab9aae26c first. Replayed with git rebase --onto 13eee7eacf8a6fcd4bb77ff680d18f727fca1171 9ce190247edbdb508b452e8c62a63c4c2cdd0089.

SHA
old head 417707ea34733fbed01b72cd4819cd23e0d851dd
new head 4450e1ddae6a3ea1c103a189988447fac8829737
new base (#19870) 13eee7eacf8a6fcd4bb77ff680d18f727fca1171

No conflicts.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • refreshRelayDemandBestEffort helper — a new best-effort wrapper (try/catch → console.warn) for demand refreshes, placed in relay-demand-ledger.ts to avoid growing desktop-relay-service.ts past its max-lines ceiling.
  • withTransientDemand containment — both refreshDemand() calls (pre-acquire and finally teardown) now route through the helper, so a failing wake signal can no longer strand the transient ref or replace the operation's result.
  • cause preservation — the mid-operation policy-flip rethrow now carries the original error as cause instead of discarding it.
  • Tests — a new spec with four exact assertions covering teardown failure, pre-op throw (no stranded ref), success preservation, and cause propagation.

The change is well-scoped and correctly reasoned: the pre-op refreshDemand failure leaked a no-expiry transient ref, and the teardown failure overwrote the operation result in both directions. The containment helper is idiomatic — console.warn('[relay] …') matches the module's existing log style, and new Error(msg, { cause }) is already used in RPC error-code paths (browser-network-tunnel.ts). The tests assert exact outcomes (message equality, call counts) rather than truthiness, and would fail if the fix were reverted. One deliberate scoping note: other bare refreshDemand() wake-signal call sites (start, authMutated, demandStateChanged, etc.) are left unwrapped, but none of them strand a ref or overwrite a result, so limiting the fix to withTransientDemand is the right call.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@nwparker
nwparker force-pushed the fix/18211-lan-mode-applies-to-paired-devices branch from 9ce1902 to 13eee7e Compare September 11, 2026 08:10
`withTransientDemand` called `refreshDemand()` bare on both sides of the
operation. It reaches the device registry (`nextPendingExpiry` -> `listDevices`)
and the settings store (`hasDemand` -> `isRelayAllowedForDevice`), so it can
fail on its own — and measured, each call site failed the operation instead:

- the pre-call threw with the transient ref already acquired, so the ref was
  never released and the operation never ran. Transient refs have no expiry, so
  that ref holds relay demand for the rest of the process.
- the teardown call replaced the operation's own result in both directions: a
  named mint failure arrived as `listDevices exploded`, and a SUCCESSFUL mint
  arrived as a rejection.

Both are wake signals; the liveness tick and the next refresh re-ask, so a lost
signal is recoverable where a stranded ref is not. The containment lives beside
the ledger that owns the ref, because desktop-relay-service.ts is at its
max-lines ceiling and this must not cost it a line.

Also carries the original error as the `cause` of the mid-operation policy flip
rewrite, which was discarding it — the one place in a commit about naming causes
that destroyed one.
@nwparker
nwparker force-pushed the nwparker/relay-transient-demand-wake branch from 417707e to 4450e1d Compare September 11, 2026 08:13
One conflict, in the desktop-relay-service import block: keep this branch's
refreshRelayDemandBestEffort import alongside the base's move to
relay-region-preference-reader (main renamed the module).
Four of the eleven findings were removable rather than suppressible: annotate
the variable instead of asserting the literal. The rest carry a line-specific
SAFETY rationale on the line that opens the cast expression, which is where the
diagnostic points -- not the `} as Foo` that closes it.

Measured: gate exit 0, tc exit 0, relay 32 files / 251 tests passed.

This branch has not been deployed

No deployments
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