Skip to content

fix(devserver): close presumed-dead /rc connections via server-side keep-alive timeout - #24213

Merged
dr1rrb merged 1 commit into
masterfrom
dev/devserver-keepalive-timeout
Aug 28, 2026
Merged

dr1rrb merged 1 commit into
masterfrom
dev/devserver-keepalive-timeout

Conversation

@dr1rrb

@dr1rrb dr1rrb commented Aug 26, 2026

Copy link
Copy Markdown
Member

GitHub Issue: closes #24206

PR Type:

🐞 Bugfix

What changed? 🚀

Every connected client already sends a KeepAliveMessage every 30 s, but the server only ever answered pings — it never used their absence to notice a dead peer. A half-open /rc socket (a client crash, a network drop, or a client that reconnected on a new socket without closing the old one) therefore kept its connection scope — and the hot-reload workspace / EnC session it owns (~hundreds of MB) — alive indefinitely (see #24206 / #24151).

RemoteControlServer.HandleConnectionAsync now bounds each receive with a watchdog: if no frame arrives for more than 2.1× the keep-alive interval, the peer is presumed dead and the connection is torn down, so its scope (and the workspace it owns) is disposed instead of lingering. A healthy client pings every 30 s — well within the ~63 s window — so only silent / half-open connections are reaped.

The interval is now defined once and shared, so the two ends can't drift:

  • KeepAliveMessage.Interval = TimeSpan.FromSeconds(30) — single source of truth. Messages/KeepAliveMessage.cs is already <Compile Link>-ed into Uno.UI.RemoteControl.ServerCore, so client and server compile the exact same value (no new csproj plumbing).
  • RemoteControlClient's keep-alive timer now reads KeepAliveMessage.Interval.
  • RemoteControlServer.HandleConnectionAsync: per-receive timeout of KeepAliveMessage.Interval * 2.1; on timeout it logs a warning and closes the connection (→ scope dispose → EndSession + workspace dispose).

PR Checklist ✅

  • 🧪 Automated test — a fast test would require the interval to be injectable (it is a shared compile-time constant today). The change is small and validated by build + review; happy to add a configurable timeout + regression test if preferred.
  • 📚 Docs — N/A.
  • 🖼️ Screenshots — N/A (no UI change).
  • ❗ Contains NO breaking changes (healthy clients are unaffected).
  • 👀 Reviewed 2 other open pull requests.

Copilot AI lite review requested due to automatic review settings August 26, 2026 17:24
@github-actions

github-actions Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @dr1rrb's task in 3m 22s —— View job


Code Review

  • Read changed files
  • Analyze for correctness, async/cancellation discipline, and code style
  • Post inline findings
  • Post summary

Summary

Verdict: ship — the fix is correct and well-structured. One nit posted inline; test gap acknowledged below.

What the change does well:

  • Single source of truth. Moving the 30-second interval into KeepAliveMessage.Interval and sharing it via <Compile Link> means client and server can never silently drift apart. The RemoteControlClient field update is clean.
  • Cancellation discipline is sound. The CancellationTokenSource.CreateLinkedTokenSource(ct) + CancelAfter pattern is the correct way to impose a per-receive deadline without polluting the outer token. The when (receiveCts.IsCancellationRequested && !ct.IsCancellationRequested) filter correctly distinguishes the timeout path from a legitimate outer shutdown — the inherent tiny race between those two checks is benign (the connection is torn down either way; only the log message might say "presumed-dead" for a normal teardown in an extreme edge case). using on the linked CTS guarantees disposal in all paths.
  • No allocation concern for this context. Creating a new CancellationTokenSource per receive frame (~every 30 s per connection) is trivial for a dev-server session.

Findings:

Severity Count
Nit 1
  • Nit (RemoteControlServer.cs:118) — the "2.1x" magic string in the log message would drift silently if the multiplier changes. Inline comment posted with a suggested fix (extract to a named constant, pass as a structured-log argument).

Test gap (acknowledged in checklist): The unchecked test item is the right call to flag. The change is small enough that build + review suffices for now, but a regression test — one that injects a configurable timeout and verifies the connection scope is disposed when no frames arrive — would give durable protection. Happy to help author one if wanted.

Copilot AI 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.

Pull request overview

This PR fixes DevServer /rc connection leaks by adding a server-side receive watchdog: if no frames arrive for ~2.1× the keep-alive interval, the server presumes the peer is dead and exits the connection loop so the scoped workspace/session is disposed.

Changes:

  • Centralizes the keep-alive cadence into KeepAliveMessage.Interval and reuses it in the client.
  • Adds a per-receive timeout in RemoteControlServer.HandleConnectionAsync to close silent/half-open connections.
  • Documents the shared interval and the server-side timeout rationale.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Uno.UI.RemoteControl/RemoteControlClient.cs Switches client keep-alive timer to use the shared KeepAliveMessage.Interval.
src/Uno.UI.RemoteControl/Messages/KeepAliveMessage.cs Introduces shared Interval constant and documents its role across client/server.
src/Uno.UI.RemoteControl.ServerCore/RemoteControlServer.cs Adds server-side receive timeout watchdog to reap presumed-dead connections.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@unodevops

Copy link
Copy Markdown
Contributor

…eep-alive timeout

The client already sends a KeepAliveMessage every 30s, but the server only
answered pings; it never used their absence to detect a dead peer. A half-open
/rc socket (client crash, network drop, or a reconnect that abandons the old
socket) therefore kept its connection scope - and the hot-reload workspace it
owns - alive indefinitely.

The server now bounds each receive: if no frame arrives for more than
KeepAliveTimeoutFactor (2.1) times the keep-alive interval, the connection is
closed so its scope (and the workspace / EnC session) is disposed. The interval
is defined once on KeepAliveMessage (the file is linked into both client and
server) so the two ends cannot drift. The timeout is overridable via
configuration so it can be regression-tested with a sub-second window.

Fixes #24206
@dr1rrb

dr1rrb commented Aug 26, 2026

Copy link
Copy Markdown
Member Author

Thanks for the review — both points addressed (branch updated):

  • Nit (2.1x magic string): extracted a named constant KeepAliveTimeoutFactor and thread it into the structured log ({Factor}), so the message can no longer drift from the multiplier.
  • Test gap: the receive timeout is now overridable via configuration (RemoteControlServer.KeepAliveTimeoutConfigurationKey), and a new regression test Given_KeepAliveTimeout drives a silent in-process connection with a sub-second timeout and asserts the server reaps it (client receive completes with null). Passes locally.

The branch was amended into a single clean commit.

@dr1rrb
dr1rrb force-pushed the dev/devserver-keepalive-timeout branch from 5dec90f to 6f057b7 Compare August 26, 2026 18:49
Copilot AI review requested due to automatic review settings August 26, 2026 18:49
@github-actions

github-actions Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @dr1rrb's task in 3m 19s —— View job


Code Review

  • Read changed files
  • Analyze for correctness, async/cancellation discipline, and code style
  • Post inline findings
  • Post summary

Summary

Verdict: ship — the previous nit is addressed and the regression test has been added.

What changed since the prior review:

  • KeepAliveTimeoutFactor = 2.1 is now a named constant (line 45), and the log message passes it as {Factor} — exactly the suggested fix. ✅
  • Given_KeepAliveTimeout test added: injects a 300 ms watchdog via KeepAliveTimeoutConfigurationKey, never sends a frame, and asserts ReceiveAsync returns null once the server reaps the connection. The AwesomeAssertions global using is already in GlobalUsings.cs so there's no missing using. ✅

Remaining observations (cosmetic, non-blocking):

Severity Description
Nit RemoteControlServer.cs:109 — the block comment still writes "2.1x" literally; if KeepAliveTimeoutFactor is ever changed, this prose won't auto-update alongside the log message

Everything else — linked-source single source of truth for KeepAliveMessage.Interval, CTS discipline in the receive loop, when (receiveCts.IsCancellationRequested && !ct.IsCancellationRequested) filter, configureAwait(false) on CloseAsync, and ResolveKeepAliveTimeout() config-override path — all look correct.

Copilot AI 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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

@unodevops

Copy link
Copy Markdown
Contributor

@dr1rrb
dr1rrb enabled auto-merge August 26, 2026 21:10
@dr1rrb
dr1rrb merged commit 042158c into master Aug 28, 2026
70 checks passed
@dr1rrb
dr1rrb deleted the dev/devserver-keepalive-timeout branch August 28, 2026 12:19
@dr1rrb

dr1rrb commented Sep 4, 2026

Copy link
Copy Markdown
Member Author

@Mergifyio backport release/stable/6.7

@mergify

mergify Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

backport release/stable/6.7

✅ Backports have been created

Details

agneszitte added a commit that referenced this pull request Sep 11, 2026
…6.7/pr-24213

fix(devserver): close presumed-dead /rc connections via server-side keep-alive timeout (backport #24213)
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.

DevServer: server-side WebSocket keepalive/timeout on /rc to reap half-open connections

4 participants