Skip to content

Fix two CI flakes: a job claim renewed twice on one clock reading, and keepCompany's context closing into the next test - #1048

Merged
lavaman131 merged 2 commits into
mainfrom
fix/ci-flakes
Dec 1, 2026
Merged

lavaman131 merged 2 commits into
mainfrom
fix/ci-flakes

Conversation

@lavaman131

Copy link
Copy Markdown
Collaborator

Two CI flakes, each fixed at its cause and covered by a test or a reproduction. One commit for each.

1. A background job claim renewed twice on one clock reading (a real runner bug)

apps/server/src/jobs/runner.test.ts "renews a claim after delayed owner resolution without reclaiming the attempt" failed on #1043 (run 46540622380) with attempts: 2, failures: 0 and only attempt 2's progress.

Cause. JobRunner renews a claim from two places: the heartbeat timer, and #execute once the owner resolves, to record the owner's binding. When both run on the same millisecond (the owner resolving just as the heartbeat fires), storage refuses the second renewal, because a renewal must move the expiry on (expiresAt <= found.claimExpiresAt in storage/memory/jobs.ts:238 and storage/postgres/jobs.ts:427). If the binding renewal comes second, the attempt is dropped as claim-lost and the job stays running until its claim expires, then is claimed again, which spends a second attempt. That matches the CI signature. If the heartbeat comes second, a heartbeat-lost failure is counted instead. The race predates #1043 and #1044: the runner has not changed since August. CI exposed it because CI's slower timers let the 100 ms heartbeat and the 150 ms owner sleep fire together.

Evidence. Stalling the event loop for 80 ms across the first heartbeat reproduced the CI failure 10 out of 10 times: attempts 2, failures 0, and a 1775 ms runtime against CI's 1780 ms. Logging the renewals showed both at the same millisecond, with the second refused as "changed before its claim renewal". Without a stall, 200 runs passed.

Fix. Both renewals now go through one #renew helper, which renews to at least 1 ms past the claim's current expiry. The test now drives the runner's injectable RunnerScheduler instead of Bun.sleep, and resolves the owner on the clock reading of the first heartbeat.

  • Before the fix: 20 out of 20 runs fail.
  • After the fix: 500 out of 500 pass, and 1600 out of 1600 pass (8 parallel runs of 200, pinned to 2 cores beside 4 CPU burners).
  • The whole runner file passes 100 times in a row (2100 tests).

2. "conversation plan": keepCompany's context still closing when the next test starts (a test problem)

Main runs 46540012615 and 46490446036 each failed one test in 0 ms with Error: _wrapApiCall: Target page, context or browser has been closed and no stack. The failure was in the test's first hook, which only touches files (decision-prose.e2e.ts:347 and decision-evidence.e2e.ts:348). In both runs the test before it used keepCompany.

Cause. keepCompany (e2e/jev-wire.ts) closed its second writer's context from the page context's close event and did not wait for that close. As each test starts, Playwright starts a trace chunk in every context it still lists (playwright/lib/index.js:637 and :720). A context whose close was finishing at that moment made tracing.startChunk fail the next test. Only this job runs with trace: "retain-on-failure" and retries: 0. The main suite traces only on a retry.

Evidence.

  • Locally, the company context's close finished only 10–20 ms before the next test's first hook.
  • In a minimal Playwright reproduction that delays that close by 4–7 ms, the next test failed with the same message in 0 ms in 11 of 240 runs. With this change it failed in 0 of 240.

Fix. The join fixture now closes every context the test opened, keepCompany's included, and waits for them. keepCompany no longer closes its own.

Verification

  • The conversation-plan suite, run locally against PostgreSQL 17 with the CI configuration: 3 full runs (255 out of 255 passed), and the two failing pairs repeated 15 times each (60 out of 60 passed).
  • bun run fix, bun run ci and bun run types are clean.
  • bun test: everything passes except two tests that fail only on this machine, whose global git config requires a signing key. Both pass with GIT_CONFIG_GLOBAL=/dev/null.

…ne clock reading both land

A heartbeat and the renewal that records the owner's binding can run on the same
millisecond (the owner resolving just as the heartbeat's timer fires). Storage
refuses a renewal that does not move the claim's expiry on, so the second one
failed as a conflict: the attempt was dropped as claim-lost (or, the other way
round, counted a heartbeat-lost failure) and the job sat running until its claim
expired and was claimed again, spending a second attempt. CI hit it as the
runner test "renews a claim after delayed owner resolution" ending with
attempts 2.

Both renewals now go through one helper that renews to at least a millisecond
past the claim's current expiry. The test drives the runner's injectable
scheduler and lands the owner on the first heartbeat's clock reading, which
failed every time before the change.
keepCompany closed its second writer's context from the page context's close
event without waiting for it, so that close could still be running when the next
test began. Playwright starts a trace chunk in every context still open as a
test starts, and with the conversation-plan suite's retain-on-failure tracing a
context that was closing just then failed the next test before its first line:
"_wrapApiCall: Target page, context or browser has been closed" in 0ms, always
right after a test that kept company (decision-prose and decision-evidence on
main).

The join fixture now closes every context the test opened, keepCompany's
included, and waits for them; keepCompany no longer closes its own.
@lavaman131
lavaman131 merged commit 136128e into main Dec 1, 2026
26 checks passed
@lavaman131
lavaman131 deleted the fix/ci-flakes branch December 1, 2026 01:36
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