Skip to content

Fix flaky socket-readable test on loopback - #570

Queued
npapagna wants to merge 3 commits into
mainfrom
npapagna/fix-flaky-socket-readable-test
Queued

Fix flaky socket-readable test on loopback#570
npapagna wants to merge 3 commits into
mainfrom
npapagna/fix-flaky-socket-readable-test

Conversation

@npapagna

@npapagna npapagna commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • The "reports a socket readable when data is actually waiting on it" test in nativeSocketLibrary.test.ts asserted readiness immediately after the server-side write resolved, assuming loopback delivery to the client's kernel receive buffer was instantaneous. Under CI load that's not guaranteed, causing an intermittent failure (seen in this run).
  • Switched the assertion to vi.waitFor, matching the existing "positive assertion, eventually true" pattern used elsewhere in the client suite, instead of adding retry logic to isReadable() itself, which needs to stay a true non-blocking, single-shot poll for its future callers.
  • Corrected the loopbackConnection.ts doc comment that had claimed the client side always "already seen it" by the time writeFromServer resolves.

Test plan

  • npx vitest run src/sockets/__tests__/nativeSocketLibrary.test.ts (client workspace)
  • npm run test:client (full client suite, 459 test files passed)
  • npm run lint && npm run format:check && npm run compile

🤖 Generated with Claude Code

The "reports a socket readable when data is actually waiting on it"
test asserted isReadable() immediately after the server-side write
resolved, assuming the bytes had already landed in the client's
kernel receive buffer at that point. That's not guaranteed: the
write callback only confirms the server flushed the send, not that
loopback delivery to the client's socket completed, and under CI
load the two can be a beat apart.

Poll for readiness with vi.waitFor instead of asserting once. This
matches the "positive assertion, eventually true" pattern used
elsewhere in the client test suite, and keeps isReadable() itself
unchanged: it's meant to be a true non-blocking, single-shot check
(poll with a 0 timeout), so retry logic belongs in the test, not in
the thing under test.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@npapagna
npapagna force-pushed the npapagna/fix-flaky-socket-readable-test branch from 76c6e6a to c283352 Compare September 7, 2026 20:13
Comment thread client/src/sockets/__tests__/support/loopbackConnection.ts Outdated
npapagna and others added 2 commits September 7, 2026 18:03
Address review feedback on #570: the "resolving doesn't mean the
client side has seen it" caveat only covered writeFromServer, but
resetFromServer has the identical gap (already noted, but buried
inside the POSIX-gated reset test's own comment as the reason that
assertion is Windows-only). Folding it into this doc comment makes
it the one place both caveats live, instead of leaving the second
one stranded in an unrelated test.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The Windows-only "reports the socket as unusable when it has been
reset by its peer" test carried a comment claiming the same
assertion (isReadable throwing) would also hold on POSIX, just too
flaky to test given RST delivery timing. Verified live via poll() on
macOS: that's not accurate. POSIX poll() leaves POLLIN set alongside
POLLHUP/POLLERR for a reset connection, so isReadable() reports it
readable there instead of throwing, unlike WSAPoll, which clears
POLLRDNORM. That's a platform semantic difference, not a timing
race, so no POSIX equivalent of that test existed at all before now.

Corrected the comment and added the POSIX case, polling with
vi.waitFor since the RST still needs a moment to reach and be
processed by the client fd's kernel state, the same timing gap this
PR is already about.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@npapagna
npapagna added this pull request to the merge queue Sep 7, 2026
Any commits made after this event will not be merged.
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.

2 participants