Skip to content

test: wait for the condition in two flaky tests - #1293

Merged
amiddavid merged 1 commit into
mainfrom
test/1268-flaky-tests
Nov 23, 2026
Merged

amiddavid merged 1 commit into
mainfrom
test/1268-flaky-tests

Conversation

@vz-ibm

@vz-ibm vz-ibm commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

Two tests failed once in CI and passed on a rerun (#1268). Both waited for the wrong condition. This PR makes each test wait for the condition it needs. No production code changes. No assertion changes.

Fixes #1268

What we found

  1. TestFallbackReasonIsLogged (proxy). The dash writer adds to its Written count before it calls the test-only Tap hook (dash/capture.go). So waitForRows(t, rec, 2) can return while the tap holds only the first event, and evs[0] panics (index out of range [0] with length 0).
  2. failopen "a restart that is healthy before the 5 s cap…" (OpenCode plugin). The issue's guess was wrong. The failing send is the one before the restart, while the proxy is down. waitUntilUnhealthy returned on any failed /healthz, including a reset from a proxy that was still exiting. The plugin falls back to the provider only on a refused connection, on purpose (a reset is never replayed). So the next send could reach the dying proxy, get a reset, and fail with fetch failed.

What changed

  • proxy/accountant_wire_test.go: new accTap.waitAll(t, n), a poll with a time limit (testutil.WaitForState). accRunUsageBody uses it, so every caller of accRunUsage gets the fix.
  • opencode-plugin/test/helpers.mjs: waitUntilUnhealthy polls a raw TCP connect every 50 ms, with a 10 s limit, until the port refuses. That is the condition the plugin needs before it falls back. About 45 tests use this helper.

Behavior change

None. Tests only.

Data dependence

None.

How it was tested

  • Both failures reproduced with a temporary change, then removed:
    • a 20 ms delay before the Tap loop gave the same panic; with the fix and the delay, 20 of 20 pass;
    • a stub proxy that resets every connection for 300 ms after SIGTERM gave the exact CI stack (fetch failed at failopen.test.mjs:487); with the fix, 20 of 20 pass, and the whole failopen file 19 of 19.
  • go test -race -run 'TestFallbackReasonIsLogged$' -count=200 ./proxy/: pass.
  • CGO_ENABLED=1 go test -race -timeout 40m ./proxy/: pass. make lint: pass.
  • Plugin suite: 113 of 113 pass. The two failopen tests in CI order: 100 of 100.

Open questions

  • About 17 other proxy tests call waitForRows and then read tap.all(), with the same race (in accountant_fallbacks, _presets, _rounds, _responses, metricsink, rejected_savings, tooldefer_ledger, tooldefer_stub_body). With the delay in, TestTailBreakpointReportsItsBytes panicked the same way. Some loops can also check fewer events than they should and still pass. Filed as test: about 17 proxy tests read the dash tap before it holds every event #1292, outside this PR.
  • The plugin tests use fixed ports, so two worktrees that run them at the same time can clash (EADDRINUSE). That is a test-setup point, not a product bug.

Assisted-By: Claude (Anthropic AI) [email protected]

Two tests waited for the wrong condition and failed once in CI.

- opencode-plugin: waitUntilUnhealthy returned on any failed /healthz,
  for example a reset from a proxy that is still exiting. The plugin
  sends a request to the provider only when the connection is refused,
  so the next send could get a reset and "fetch failed". The helper now
  polls, with a 10 s limit, until a new connection is refused.
- proxy: the dash writer counts a batch as written before it calls Tap.
  waitForRows could return before the tap held the second event, so
  evs[0] was out of range. accRunUsageBody now also waits, with a time
  limit, until the tap holds both events.

Fixes #1268

Signed-off-by: Vitaly Zabershinsky <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>

@vz-ibm vz-ibm left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (claude, user of @vz-ibm) Claude Code 2.1.325 offload-review skill, @vz-ibm's session fresh 2 of 2 1 of 2 9a158c1182f6 approve

Verdict: approve

Findings

None. Both fixes wait for the correct condition, and both have a time limit.

Non-blocking

  • proxy/accountant_wire_test.go:114 — the same race stays in TestNoUsageMeansNoAccountantResult.
    • The test calls waitForRows(t, rec, 1) and then loops over tap.all().
    • The writer adds to written before it calls Tap (dash/capture.go:578 and :589-590). So tap.all() can be empty.
    • It does not flake. The loop runs zero times and the test passes without a check.
    • Fix: use tap.waitAll(t, 1) there too. Then the test always checks the event.

What I checked

  • Read the full diff (2 files, +26 −8) and the commit message. Read issue #1268.
  • Go fix. dash/capture.go:576-594: after insertBatch, the writer calls r.written.Add first, then Tap for each event. waitForRows reads Stats().Written, so it can return before Tap runs. The new waitAll waits on the tap length with testutil.WaitForState. accTap.all() takes the mutex, so the read is safe under -race. TestFallbackReasonIsLogged (accountant_shadow_test.go:32-33) reads evs[0] from accRunUsage, so this change covers the panic in the issue. It uses the repo's wait helper, so internal/timingguard has no new case.
  • JS fix. The plugin sends the request direct only on ECONNREFUSED / ConnectionRefused (opencode-plugin/index.js:949-990). Any other error, for example a reset, goes up as fetch failed. That matches the failure at failopen.test.mjs:487. The old helper returned on any /healthz error, a reset included. The new helper returns only when a new TCP connection is refused. That is the exact condition the next send() needs.
  • Socket handling in the new helper: connect and timeout destroy the socket. error destroys it by itself. Each path resolves one time. A reset or a timeout loops again. After 10 s, assert.fail stops the loop.
  • All callers of waitUntilUnhealthy (about 50, in 11 test files) first kill the proxy (SIGTERM or the kill scripts). None of them expects a hung proxy that keeps its listener, so the stricter condition breaks no caller. stopRecorded in versions.test.mjs:84-87 is the same pattern.
  • internal/timingguard scans only *_test.go, so the 50 ms setTimeout in helpers.mjs is outside its scope.
  • No product code changes. No data-dependence row and no behavior change is needed.
  • I did not run the tests. Live tests are not approved for this review.

@vz-ibm vz-ibm left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (claude, user of @vz-ibm) Claude Code 2.1.325 offload-review skill, @vz-ibm's session fresh 1 of 2 1 of 2 9a158c1182f6 approve

Verdict: approve

Findings

None.

Non-blocking

  • The same tap race stays in other tests. dash/capture.go:580-591 adds to written before it calls Tap. So every test that reads tap.all() right after waitForRows has the same race as accRunUsageBody. Some of them:
    • proxy/accountant_rounds_test.go:48-49 reads tap.all()[0]. It can panic in the same way as #1268.
    • proxy/accountant_fallbacks_test.go:75-76, 121-122, 231-232, 332-334 index into tap.all().
    • proxy/accountant_wire_test.go:114-115, accountant_presets_test.go:73-74, accountant_fallbacks_test.go:157-158, 203-204 and accountant_responses_test.go:265 loop over tap.all(). If the tap is still empty, the loop runs 0 times and the test passes without checking anything.
    • Proposal, as a follow-up: use tap.waitAll(t, n) at these sites. You can also add one helper that waits for the rows and the tap together. The PR does not need to fix these to merge. Its scope is the two tests in #1268.
  • accTap.waitAll waits for >= n events, and accRunUsageBody then uses [1:]. This is correct, because the handler gets exactly 2 posts.
  • waitUntilUnhealthy accepts only ECONNREFUSED. Bun gives ConnectionRefused on fetch errors (index.js:950). Under Node, net.connect gives ECONNREFUSED, and the tests run under Node, so this is correct today. If the tests move to Bun, check the code there.

What I checked

  • I read the whole diff (2 files, +26/-8) and the commit message.
  • Go fix. In dash/capture.go:577-591, r.written.Add runs before the Tap loop. So waitForRows (proxy/dash_test.go:173) can return while the tap still holds fewer events. waitAll uses testutil.WaitForState, which follows the timing rule in CLAUDE.md. accTap.all() holds the lock and returns a copy, so the poll is free of races.
  • The failing Go test. TestFallbackReasonIsLogged (proxy/accountant_shadow_test.go:33) calls accRunUsage → accRunUsageBody, so the fix covers the panic in #1268. Its acc_fallback log count does not race. logFallback runs in the handler (proxy/dashcapture.go:1000), before the writer stores the event.
  • JS fix. The failure in #1268 is at failopen.test.mjs:487, the project.send() right after waitUntilUnhealthy. The plugin falls back to the provider only on a refused connection (index.js:947-951). So a reset from a proxy that is still exiting gives "fetch failed". The new helper returns only on ECONNREFUSED, which matches the condition the plugin needs. The PR's diagnosis is more exact than the guess in the issue, and I agree with it.
  • Socket handling. On connect and on timeout, the helper closes the socket. On error, Node closes it. Each attempt resolves exactly one time. The 500 ms connect timeout and the 10 s deadline give a bound on the wait.
  • Call sites. I read the waitUntilUnhealthy calls in capture.test.mjs, tui.test.mjs and failopen.test.mjs. Each call follows a SIGTERM to the proxy that holds that port. No call expects a new proxy on the same port before the wait ends, so the stricter condition cannot make these calls hang. When the kill in a try fails and nothing listens, the first connect is refused, and the helper returns at once.
  • internal/timingguard scans only _test.go files. The 50 ms setTimeout in the .mjs helper is a poll interval in a bounded loop, not a wait on time.
  • No library or proxy code changes. There is no behavior change and no change to data dependence.
  • I did not run tests. Live tests are not approved, and Go is not installed here.

@amiddavid

Copy link
Copy Markdown
Collaborator

For the record: the same failopen flake hit #1295 twice in a row on 2026-11-23 (run 45154231468). This time tests 33 and 34 failed, and test 36 failed after them. Tests 33 and 34 both call waitUntilUnhealthy right before the failing send, so this PR's helper fix covers them. Details are on #1268.

@amiddavid
amiddavid merged commit d0dea8b into main Nov 23, 2026
9 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

test: two flaky tests failed once in CI and passed on rerun

2 participants