sweepSoon starts its sweep on the next turn of the loop, so "sweeping a checkout hands the loop back before it finishes (#7688)" stops flaking
Seen: pull-17221's check 01m8swwsesn4ka5asendwf9vnw (23:08 UTC Dec 1) failed on "sweeping a checkout hands the loop back before it finishes (#7688)" in test/checks-home-sweep.test.ts. It has shown up red on other pulls' checks today too, while 178+ shards shared the machine.
Why it flakes:sweepSoon (src/checks/sweep.ts) calls sweepCheckouts straight away. Its first stretch runs in the caller's turn: readdirSync, getRun, and then removeRunDir, which hands the first rm to the thread pool before the first await. So by the time sweepSoon returns, the home's removal is already running on another thread. The test's next line, expect(existsSync(home(done.id))).toBe(true), races that thread. When the machine is busy and the test's thread is paused, the pool deletes the small home first and the assertion fails. "the abandoned-checks timer sweeps a finished run's home" has the same race, because requeueAbandonedChecks calls sweepSoon too.
Fix (src/checks/sweep.ts, sweepSoon only): start the sweep on the next turn of the loop. Then nothing of it runs in the caller's turn, not even its listing. That is what #7688 asked for: the caller, and whatever was waiting behind it (a /healthz), go first.
/** The next turn of the loop: what was waiting on it (a request, a timer) goes first. */const nextTurn =():Promise<void>=>newPromise((resolve)=>setImmediate(resolve));exportfunctionsweepSoon(app:App, now:number=Date.now()):Promise<void>{// Nothing of the sweep runs in the caller's turn, not even its listing (#7688): it starts on the// next turn of the loop, so the caller, and what was waiting behind it, go first.
sweeping ??=nextTurn().then(()=>sweepCheckouts(app, now)).catch((error)=> log.error("could not sweep the checkouts",{ error:String(error)})).finally(()=>{
sweeping =undefined;});return sweeping;}
sweepCheckouts and reclaimIfFull stay as they are, since their callers await them.
Test (test/checks-home-sweep.test.ts): replace the body of "sweeping a checkout hands the loop back before it finishes (#7688)". It now makes the home after the call, so it fails on main every time, not just sometimes: main's sweep has already listed the directory, so it never sees the home. With the fix it passes every time.
test("sweeping a checkout hands the loop back before it finishes (#7688)",async()=>{const done =run();claimRun(app.db, done.id);finishRun(app.db, done.id,{ state:"ok", summary:"ok"});const sweep =sweepSoon(app);// Nothing of the sweep runs in the caller's turn, not even its listing: a home made after the// call is one it removes, and none is touched before the caller hands the loop back.populate(done.id);expect(existsSync(home(done.id))).toBe(true);await sweep;expect(existsSync(home(done.id))).toBe(false);});
No limit, timeout or retry changes. The test is strictly stronger than before.
Check when applying: checks-home-sweep, checks-restart, checks-disk-red and any test that calls requeueAbandonedChecks or startChecks with fake timers. At boot, startChecks now runs adoptRunnerJobs and the first schedule before the sweep lists the directory. That is safe as long as a checkout is only made for a run that is already claimed, which the 30-second timer's sweep already relies on.
Done when: main and three pull checks in a row pass this test with no rerun.
sweepSoon starts its sweep on the next turn of the loop, so "sweeping a checkout hands the loop back before it finishes (#7688)" stops flaking
Seen: pull-17221's check 01m8swwsesn4ka5asendwf9vnw (23:08 UTC Dec 1) failed on "sweeping a checkout hands the loop back before it finishes (#7688)" in test/checks-home-sweep.test.ts. It has shown up red on other pulls' checks today too, while 178+ shards shared the machine.
Why it flakes:
sweepSoon(src/checks/sweep.ts) callssweepCheckoutsstraight away. Its first stretch runs in the caller's turn:readdirSync,getRun, and thenremoveRunDir, which hands the firstrmto the thread pool before the firstawait. So by the timesweepSoonreturns, the home's removal is already running on another thread. The test's next line,expect(existsSync(home(done.id))).toBe(true), races that thread. When the machine is busy and the test's thread is paused, the pool deletes the small home first and the assertion fails. "the abandoned-checks timer sweeps a finished run's home" has the same race, becauserequeueAbandonedCheckscallssweepSoontoo.Fix (src/checks/sweep.ts,
sweepSoononly): start the sweep on the next turn of the loop. Then nothing of it runs in the caller's turn, not even its listing. That is what #7688 asked for: the caller, and whatever was waiting behind it (a /healthz), go first.sweepCheckoutsandreclaimIfFullstay as they are, since their callers await them.Test (test/checks-home-sweep.test.ts): replace the body of "sweeping a checkout hands the loop back before it finishes (#7688)". It now makes the home after the call, so it fails on main every time, not just sometimes: main's sweep has already listed the directory, so it never sees the home. With the fix it passes every time.
No limit, timeout or retry changes. The test is strictly stronger than before.
Check when applying: checks-home-sweep, checks-restart, checks-disk-red and any test that calls
requeueAbandonedChecksorstartCheckswith fake timers. At boot,startChecksnow runsadoptRunnerJobsand the firstschedulebefore the sweep lists the directory. That is safe as long as a checkout is only made for a run that is already claimed, which the 30-second timer's sweep already relies on.Done when: main and three pull checks in a row pass this test with no rerun.