Repository navigation
test: wait for the condition in two flaky tests - #1293
Merged
Merged
Conversation
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
commented
Nov 23, 2026
vz-ibm
left a comment
Collaborator
Author
There was a problem hiding this comment.
| 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 overtap.all(). - The writer adds to
writtenbefore it callsTap(dash/capture.go:578and:589-590). Sotap.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.
- The test calls
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: afterinsertBatch, the writer callsr.written.Addfirst, thenTapfor each event.waitForRowsreadsStats().Written, so it can return beforeTapruns. The newwaitAllwaits on the tap length withtestutil.WaitForState.accTap.all()takes the mutex, so the read is safe under-race.TestFallbackReasonIsLogged(accountant_shadow_test.go:32-33) readsevs[0]fromaccRunUsage, so this change covers the panic in the issue. It uses the repo's wait helper, sointernal/timingguardhas 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 asfetch failed. That matches the failure atfailopen.test.mjs:487. The old helper returned on any/healthzerror, a reset included. The new helper returns only when a new TCP connection is refused. That is the exact condition the nextsend()needs. - Socket handling in the new helper:
connectandtimeoutdestroy the socket.errordestroys it by itself. Each path resolves one time. A reset or a timeout loops again. After 10 s,assert.failstops 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.stopRecordedinversions.test.mjs:84-87is the same pattern. internal/timingguardscans only*_test.go, so the 50 mssetTimeoutinhelpers.mjsis 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
commented
Nov 23, 2026
vz-ibm
left a comment
Collaborator
Author
There was a problem hiding this comment.
| 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-591adds towrittenbefore it callsTap. So every test that readstap.all()right afterwaitForRowshas the same race asaccRunUsageBody. Some of them:proxy/accountant_rounds_test.go:48-49readstap.all()[0]. It can panic in the same way as #1268.proxy/accountant_fallbacks_test.go:75-76, 121-122, 231-232, 332-334index intotap.all().proxy/accountant_wire_test.go:114-115,accountant_presets_test.go:73-74,accountant_fallbacks_test.go:157-158, 203-204andaccountant_responses_test.go:265loop overtap.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.waitAllwaits for>= nevents, andaccRunUsageBodythen uses[1:]. This is correct, because the handler gets exactly 2 posts.waitUntilUnhealthyaccepts onlyECONNREFUSED. Bun givesConnectionRefusedon fetch errors (index.js:950). Under Node,net.connectgivesECONNREFUSED, 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.Addruns before theTaploop. SowaitForRows(proxy/dash_test.go:173) can return while the tap still holds fewer events.waitAllusestestutil.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) callsaccRunUsage→accRunUsageBody, so the fix covers the panic in #1268. Itsacc_fallbacklog count does not race.logFallbackruns 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, theproject.send()right afterwaitUntilUnhealthy. 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 onECONNREFUSED, 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
connectand ontimeout, the helper closes the socket. Onerror, 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
waitUntilUnhealthycalls incapture.test.mjs,tui.test.mjsandfailopen.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 atryfails and nothing listens, the first connect is refused, and the helper returns at once. internal/timingguardscans only_test.gofiles. The 50 mssetTimeoutin the.mjshelper 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.
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
TestFallbackReasonIsLogged(proxy). The dash writer adds to itsWrittencount before it calls the test-onlyTaphook (dash/capture.go). SowaitForRows(t, rec, 2)can return while the tap holds only the first event, andevs[0]panics (index out of range [0] with length 0).waitUntilUnhealthyreturned 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 withfetch failed.What changed
proxy/accountant_wire_test.go: newaccTap.waitAll(t, n), a poll with a time limit (testutil.WaitForState).accRunUsageBodyuses it, so every caller ofaccRunUsagegets the fix.opencode-plugin/test/helpers.mjs:waitUntilUnhealthypolls 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
Taploop gave the same panic; with the fix and the delay, 20 of 20 pass;fetch failedatfailopen.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.Open questions
waitForRowsand then readtap.all(), with the same race (inaccountant_fallbacks,_presets,_rounds,_responses,metricsink,rejected_savings,tooldefer_ledger,tooldefer_stub_body). With the delay in,TestTailBreakpointReportsItsBytespanicked 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.EADDRINUSE). That is a test-setup point, not a product bug.Assisted-By: Claude (Anthropic AI) [email protected]