Skip to content

test(gateway): stale-recovery health test can read the loopback server's accepted count before it records the new socket - #2211

Merged
bkudiess merged 1 commit into
openclaw:mainfrom
bkudiess:bkudiess-health-accept-count
Nov 14, 2026
Merged

bkudiess merged 1 commit into
openclaw:mainfrom
bkudiess:bkudiess-health-accept-count

Conversation

@bkudiess

Copy link
Copy Markdown
Collaborator

Related: #2079

What Problem This Solves

Fixes: the Core test job fails the gateway stale-recovery health test when it reads the loopback server's accepted-socket count before the server has recorded the new connection.

User Impact

User impact: no user-visible change. The Core test job no longer needs a rerun for this test. Gateway reconnect and health behavior are unchanged.

Why This Change Was Made

OpenClawGatewayClientTests.CheckHealth_StaleRecoveryCannotReplaceNewerSocket(delayedSend: True) failed once on main efb2442c (run 43539494326, job 132276558851), after 49 ms. The failure was "Assert.Equal() Failure: Expected: 2, Actual: 1" at the server's accepted-socket count. This test was added in #2079 (transport loss fails pending requests), and this change keeps it as is apart from one wait.

This is not a product race. Every assertion about the product passed before the count check:

  • The client was connected to the gateway.
  • Its session key was the newer one (agent:new:main).
  • Its handshake generation was unchanged.
  • No reconnect was authorized.

A stale recovery replacing the newer socket would cause more sockets or a different generation, not fewer.

The test server counts sockets late. LoopbackWebSocketServer adds a socket to its list only after AcceptWebSocketAsync has already sent the 101 Switching Protocols reply, in a continuation on the shared thread pool. The client's ConnectAsync returns as soon as it receives 101, so the test could read the count before the server had recorded the second socket.

Change (test only, two lines): before the exact-count assertion, the test waits for the expected count with the server's existing WaitForAcceptedCountAsync. The wait is bounded by the file's named StepTimeout (OPENCLAW_TEST_GATEWAY_STEP_TIMEOUT_SECONDS), as the file's other accepted-count checks already do. The exact-count assertion is kept, so an extra stale-recovery socket still fails the test.

Evidence

Harness: a scratch build (not committed) makes the test server record each socket 300 ms after sending 101.

Code CheckHealth_StaleRecoveryCannotReplaceNewerSocket with the slow server
Original delayedSend: True failed 3/3 with exactly Expected: 2, Actual: 1
This change Passed 6/6 (both cases, 3 runs)

On the real test server, both cases passed 50 runs: 100/100.

Review:

  • Dual-model Hanselman review: claude-opus-5.5 (high) and gpt-6-astra (long_context, high). Neither found an actionable issue.
  • Both traced the product path independently:
    • The second ConnectAsync always opens a new socket.
    • The released stale health send sees an old generation and returns without reconnecting.
    • A stale reconnect could only add a socket or change the generation.
  • Both confirmed three points:
    • Waiting for at least N sockets, then asserting exactly N, still catches an extra socket.
    • delayedSend: False has the same ordering gap, though it rarely shows.
    • This was the file's only count read without a wait.

Change Type

  • Bug fix
  • Feature
  • Refactor
  • Docs or instructions
  • Tests or validation
  • Security hardening
  • Chore or infrastructure

Scope

  • Tray or WinUI UX
  • Windows node capability
  • Local MCP or winnode
  • Gateway, connection, or pairing
  • Setup or onboarding
  • Permissions, privacy, or security
  • Tests, CI, or docs

Required proof pools

  • none: test-only change to one unit-test assertion. Product code, UI, MCP, Gateway and system.run are unchanged.

Validation

On head 0e48120e (rebased onto b713f345, whose only change is in an unrelated UI test):

  • The two CheckHealth_* cases, 10 runs: 20/20 passed.

On the same change before the rebase (base 7051c986):

  • ./build.ps1: passed.
  • dotnet test ./tests/OpenClaw.Shared.Tests/OpenClaw.Shared.Tests.csproj: 5876 passed, 29 skipped, 0 failed.
  • dotnet test ./tests/OpenClaw.Tray.Tests/OpenClaw.Tray.Tests.csproj (isolated profile): 6668 passed, 0 failed.
  • CheckHealth_StaleRecoveryCannotReplaceNewerSocket, 50 runs: 100/100 passed.
  • ./scripts/validate-test-timing-patterns.ps1 and git diff --check: passed.

Real Behavior Proof

  • Environment tested: local Windows 11 developer machine, 32 logical processors, .NET 10.0.403.
  • PR head or commit tested: 0e48120e. The harness and the full suites ran on the same change before the rebase.
  • Exact steps or command run: the harness in Evidence, plus the commands listed in Validation.
  • Evidence after fix: Passed! - Failed: 0, Passed: 2 for both cases with the slow server. The original showed Failed ... CheckHealth_StaleRecoveryCannotReplaceNewerSocket(delayedSend: True) ... Expected: 2 Actual: 1.
  • Observed result: the original failed 3/3 when the server records sockets late; this change passed 6/6.
  • Screenshot or artifact links verified? (Yes/No/N/A): N/A (unit test; output is quoted above).
  • Not verified or blocked: CI on this PR has not run yet.

Security Impact

  • New permissions or capabilities? (Yes/No): No
  • Secrets or tokens handling changed? (Yes/No): No
  • New or changed network calls? (Yes/No): No
  • Command or tool execution surface changed? (Yes/No): No
  • Data access scope changed? (Yes/No): No
  • If any answer is Yes, explain the risk and mitigation: N/A

Compatibility and Migration

  • Backward compatible? (Yes/No): Yes
  • Config or environment changes? (Yes/No): No.
  • Migration needed? (Yes/No): No
  • If yes, list the exact upgrade steps: N/A

Review Conversations

  • I replied to or resolved every bot review conversation addressed by this PR.
  • I left unresolved only conversations that still need maintainer judgment.

…r's accepted count before it records the new socket

OpenClawGatewayClientTests.CheckHealth_StaleRecoveryCannotReplaceNewerSocket
(added in openclaw#2079, "transport loss fails pending requests") failed once on main
with "Expected: 2, Actual: 1" at its accepted-socket count. The product held:
the newer socket was open, its generation and session key were unchanged, and
no reconnect was authorized. The loopback test server records a socket only
after AcceptWebSocketAsync has sent 101, on a thread-pool continuation, so the
client's ConnectAsync can return before the count includes it.

- Wait for the expected count with the server's existing
  WaitForAcceptedCountAsync (bounded by the named StepTimeout), as the file's
  other accepted-count checks do, then keep the exact-count assertion so an
  extra stale-recovery socket still fails.

Co-authored-by: Copilot App <[email protected]>
@clawsweeper

clawsweeper Bot commented Nov 14, 2026

Copy link
Copy Markdown

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@bkudiess
bkudiess merged commit 34e8b60 into openclaw:main Nov 14, 2026
29 checks passed
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