Skip to content

test(helpers): wait out a busy store delete instead of failing an unrelated test - #458

Merged
abogartz merged 3 commits into
mainfrom
fix/windows-store-teardown-retry
Nov 20, 2026
Merged

abogartz merged 3 commits into
mainfrom
fix/windows-store-teardown-retry

Conversation

@abogartz

Copy link
Copy Markdown
Owner

What failed

The Windows job on PR 457 went red on one test whose own assertions had all passed:

error: EBUSY: resource busy or locked, rm 'C:\Users\RUNNER~1\AppData\Local\Temp\osi-capture-avail-ETFah7'
    at removeStore (test/helpers/remove-store.ts:19:3)
    at <anonymous> (test/harness.ts:103:7)

capture availability tag > a control character in a candidate does not reach the audit line raw — capture and logfmt code, nothing to do with the pull changes in that PR. A rerun passed. That is the shape of this bug: the red test is whichever one happened to be tearing down.

Why

removeStore closed this process's lock connections and called rmSync with maxRetries: 5, retryDelay: 20 — about 100 ms. Two things beat that:

  • A lock connection inside a store-lock section that is still running is deliberately left open. closeAllLockDatabases skips any connection with inUse > 0, because closing it would give up a lock its writer still believes it holds, and that silently drops the exclusion the lock exists to provide.
  • Windows refuses to delete a directory containing an open file, and it reports EPERM as well as EBUSY when a child is merely mapped. macOS and Linux delete it either way, which is why nothing like this appears on the other runners.

The change

removeWhileBusy runs the delete with a doubling wait (25 ms to 400 ms) against a 2 s budget. Outcomes:

  • Frees up — deleted, as before.
  • Still busy after the budget — one stderr line naming the root, the code, the wait and the attempt count, then abandoned. The directory is inside the OS temporary tree, which the runner reclaims; a red test on an unrelated assertion is not reclaimable.
  • Anything that is not a held handle — throws immediately, unretried. A wrong delete stays loud.

removeStore(dir, warn?) keeps its old call signature, so the harness and the 230-odd existing call sites are untouched.

Deliberately not done

  • No force-close of a connection with inUse > 0. It would make the delete succeed by breaking the lock's guarantee, and at teardown that guarantee is the last thing still being tested.
  • No hunt for the task that outlived its test. That is the underlying defect and it is a separate piece of work; the warning line names the root, so the next occurrence is evidence rather than a mystery.

Tests

Four cases in test/store/temp-store-fixture.test.ts, driven through an injected remover: retried-then-clean, budget-spent-reports-and-returns-false, non-busy-throws-once, and a real removeStore on a quiet temp directory. A held handle cannot be produced on Linux or macOS — both delete a file another descriptor still has open — so a test that waited for the real thing would assert nothing off Windows.

Merge gate green at 0084d75: comment policy, typecheck, 1597 unit tests, 35 e2e.

— identified as an AI Assistant

…elated test

removeStore closed this process's lock connections and then called rmSync with
five 20 ms retries. On the Windows runner that was still not enough: a lock
connection inside a section that is still running is deliberately left open,
because closing it would give up a lock its writer believes it holds, so
closeAllLockDatabases skips it. Windows then refuses the delete, and the red
test is whichever one happened to be tearing down.

Observed on the Windows job for PR 457: `EBUSY: resource busy or locked, rm
'C:\...\osi-capture-avail-ETFah7'` from remove-store.ts:19 through the harness
afterEach, in a capture-availability test whose own assertions had all passed.

removeWhileBusy now runs the delete with a doubling wait up to a 2 s budget. A
delete that is still busy after the budget is reported on stderr naming the
root, the code and the attempts, and abandoned: the directory lives in the OS
temporary tree, which the runner reclaims, while a red test on an unrelated
assertion is not reclaimable. An error that is not a held handle still throws
immediately, so a wrong delete stays loud.

The branches are driven through an injected remover in
test/store/temp-store-fixture.test.ts, because Linux and macOS both delete a
file another descriptor still has open — a test that waited for the real thing
would assert nothing off Windows.

@WhollyMindless WhollyMindless left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I checked the same-process holder described in the PR on Windows with Bun 1.4.4 at 0084d75. In a fresh temp store, I entered withStoreLock, had its callback await a 45 ms timer, then called removeStore while the callback was pending. Teardown warned after 9 attempts and 1,975 ms of backoff. The directory still existed and the holder was still pending. In the control, awaiting the same holder before calling removeStore deleted the directory immediately.

Bun.sleepSync keeps that callback from resuming between attempts, and closeAllLockDatabases() only runs before the first attempt, when it skips the in-use connection. The warning does keep the test green. If reclaiming this kind of store during teardown is intended, the retry needs to yield to the holder and close newly idle connections before retrying. If deferring cleanup is intentional, could teardown skip the two-second wait for a known same-process holder? The injected-remover test misses this distinction because its outcome advances with the attempt count.

@WhollyMindless

Copy link
Copy Markdown
Contributor

Would a small follow-up patch for the same-process case be useful? I can make the shared test teardown yield between delete attempts so the lock holder can finish, then close newly idle connections before retrying. I’d include a Windows regression test with a real withStoreLock holder that checks the directory is actually removed. If that fits your plan for #458, I’m happy to send the patch.

… it out

The review on #458 measured the first version on Windows and was right on all
three counts. A pending `withStoreLock` callback needs the event loop, and
`Bun.sleepSync` owns the loop, so the retry window could never free the handle:
9 attempts, 1,975 ms, directory still present, holder still pending. The
control — awaiting the holder first — deleted immediately. `closeAllLockDatabases`
also ran once, before the first attempt, so a connection that went idle
mid-wait was never closed.

Two teardowns now, because the two callers differ:

- `removeStore` stays synchronous. It asks `heldLockSectionsUnder` first: with a
  section running in this process the outcome is already decided, so it reports
  at once and spends no wait. Without one, the handle is external or lingering
  and the backoff is the right tool.
- `removeStoreAsync` awaits between attempts and closes idle connections before
  each retry, which is what actually reclaims a store whose section is still
  running. `withTempStore`'s `afterEach` uses it.

`heldLockSectionsUnder(dir)` is a new seam in src/store/lock.ts beside
`heldLockDatabases`: the connections whose section is running, under one
directory. `closeAllLockDatabases` already skips those, and now a caller can see
why before deciding whether waiting can help.

The tests no longer advance on an attempt count. The lock section is a real
`withStoreLock`, and only the OS rule is injected — Linux and macOS delete a
file another descriptor still has open, so the refusal cannot be produced
portably. That makes the distinction assertable everywhere: the async teardown
yields, the holder finishes, the store goes; the synchronous one gives up with
the holder still pending and no sleep taken.
@abogartz

Copy link
Copy Markdown
Owner Author

All three points confirmed against the code, and fixed in b321d92 — no patch needed, though the measurement is what made this actionable.

The defect exactly as you described it. Bun.sleepSync owns the event loop, and a pending withStoreLock callback is a promise waiting for that loop, so the retry window could not free the handle: the wait was not merely slow, it was decided before the first gap. closeAllLockDatabases() running once before attempt one made it worse — the connection that would have been closable after the holder finished was never revisited.

Two teardowns now, because the two callers genuinely differ:

  • removeStore (sync) asks first whether this process is running a lock section under the directory. If it is, it reports immediately and takes no wait at all — your second suggestion. If nothing is held here, the handle is external or Windows lingering after a close, and the backoff is the right tool.
  • removeStoreAsync awaits between attempts and closes idle connections before each retry, so the holder resumes, its connection goes idle, and the next pass closes it. withTempStore's afterEach now uses it, which is the path that was failing.

The detection is a new seam beside heldLockDatabases: heldLockSectionsUnder(dir) in src/store/lock.ts returns the connections whose inUse is non-zero under one directory. closeAllLockDatabases already skipped those; now a caller can see why before deciding whether waiting can help.

On the tests — agreed, and that was the weakest part of the first version. The lock section is now a real withStoreLock, held open by a promise the test controls, and only the OS rule is injected (refuse while heldLockSectionsUnder(dir) is non-empty), because Linux and macOS delete a file another descriptor still has open. The two outcomes then differ on every platform:

  • async teardown → yields, the holder finishes, finished() is true, the directory is gone;
  • sync teardown → returns false with finished() still false and the sleep seam never called.

Plus the mechanics kept under injected seams: gap sequence [25, 50], closeIdle called before the first attempt and before every retry, budget exhaustion reported once with the holder count, and a non-busy code thrown unretried.

Gate green locally at b321d92: comment policy, typecheck, 1627 unit tests, 37 e2e.

— identified as an AI Assistant

@WhollyMindless WhollyMindless left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verified the fix at b321d92 on native Windows with Bun 1.4.4. I reran the original 45 ms withStoreLock case through both public teardown helpers, using the real filesystem. Sync teardown returned in 1 ms with one warning and the holder still pending. Async teardown let the holder finish and removed the directory in 77 ms without a warning. That resolves the behavior I reported.

@abogartz
abogartz merged commit 9e7c88d into main Nov 20, 2026
4 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.33.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants