Skip to content

fix(fslock): handle temporary permission errors on Windows - #586

Merged
amoeba merged 10 commits into
columnar-tech:mainfrom
amoeba:fslock/windows-delete-pending
Nov 17, 2026
Merged

amoeba merged 10 commits into
columnar-tech:mainfrom
amoeba:fslock/windows-delete-pending

Conversation

@amoeba

@amoeba amoeba commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

This PR fixes a transient error that should only be possible on Windows and affects both dbc's native and Wasm builds. The bug was introduced in #551.

The error we got in CI which led us to this was,

WORKER SMOKE FAIL: Error: dbc-wasm: failed to lock credentials file: fslock: create c:/users/runneradmin/appdata/local/temp/dbc-wasm-worker-r7a7j7/data/dbc/credentials/credentials.toml.lock.js: open c:/users/runneradmin/appdata/local/temp/dbc-wasm-worker-r7a7j7/data/dbc/credentials/credentials.toml.lock.js: Operation not permitted
    at prefixError (D:\a\dbc\dbc\packages\npm-wasm\index.cjs:31:10)
    at Worker.<anonymous> (D:\a\dbc\dbc\packages\npm-wasm\index.cjs:147:19)
    at Worker.emit (node:events:519:28)
    at MessagePort.<anonymous> (node:internal/worker:270:53)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:843:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28)

The cause is that we use a filesystem lock here and, on Windows, there's a very brief period after a file delete where trying to create a new file at that path returns a permissions error instead of a file exists error due to the handle still being held. The fix is to retry gaining the lock on such errors for an extra 250ms.

…nding

On Windows a deleted file stays "delete pending" until the deleting
handle closes, and creating it again in that window fails with
ERROR_ACCESS_DENIED (EPERM under Node) rather than ERROR_FILE_EXISTS.
acquireMarker treated that as fatal, so a waiter could fail outright
when the holder released the marker just as it retried. This flaked
the Windows worker smoke test, where two Wasm runtimes contend on the
credentials lock. Native dbc on Windows is affected too.

On Windows hosts, retry permission errors from the create until the
timeout (with a short grace so non-blocking attempts also ride out the
window), then report the original error so genuine permission problems
still surface. Add a regression test in which a rival holder takes and
releases the marker in a tight loop; it fails within a few attempts
without this change.
The grace for a delete-pending create was measured from the start of
acquireMarker, so for any timeout of 250ms or more it was just the
caller's deadline. That cut both ways: a waiter that saw the lock freed
just before its deadline failed with a raw permission error instead of
acquiring it, and a genuine access-denied error took the caller's whole
timeout (10s for credentials) to surface.

Start the grace at the first delete-pending failure, reset it on any
other outcome, and leave it independent of the caller's timeout. As a
deliberate trade-off, an acquire may now overrun its timeout by up to
the 250ms grace, and a non-blocking attempt may block for that long,
while a delete is pending.

Add Windows tests that hold a marker delete-pending deterministically
(classic disposition, kept open by a handle): one for a delete that
clears within the grace, and one for an error that never clears, which
fails against the previous code because it took the full timeout.
Two waiters that both find the same stale marker both remove it. On
Windows the second delete can land while the first is still pending and
fail with access denied, which removeDeadMarker reported as a hard
error. Treat it like the marker already being gone: retry the create,
which waits out the pending delete.

There is no deterministic test: a delete-pending marker already fails
the stat that precedes the remove, so the window between the two cannot
be held open.
@amoeba
amoeba marked this pull request as ready for review November 13, 2026 20:13
@amoeba amoeba changed the title fix(fslock): handle temporary permission errors on windows fix(fslock): handle temporary permission errors on Windows Nov 13, 2026

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The fix is right and narrowly scoped: the Wasm build gets IsWindows from the JS host, and Windows ERROR_ACCESS_DENIED and Node's EPERM both count as fs.ErrPermission, so both builds are covered. The holdDeletePending helper is a nice deterministic way to reproduce the state. One design question and a few small things.

  1. The 250ms limit applies even when the caller asked to wait longer. A blocking caller with a 30s timeout still fails after about 250ms if the pending delete lasts longer. A pending delete lasts until every open handle to the file closes, not just the deleting one. That includes antivirus or the search indexer, the heartbeat's Chtimes, and other waiters' os.Stat/os.ReadFile in removeDeadMarker, and antivirus scans can easily take more than 250ms. For native callers, acquireMarker only runs after the OS lock in the same directory is held, so a lasting permission error on the marker is almost certainly a pending delete rather than a real permission problem. Suggestion: use the 250ms limit for non-blocking calls and the Wasm build, and let native blocking callers keep retrying until their own deadline. TestAcquireMarkerReportsPersistentPermissionError would then need to use timeout 0 or the Wasm path.
  2. The comment on windowsDeleteMaybePending says "until the deleting handle closes"; it should be "until all open handles to it close".
  3. The retry ignores deadline, so a call can return up to 250ms after its timeout, and a non-blocking attempt can now block for up to 250ms. That's fine, but the acquireMarker doc comment should say so.
  4. In TestAcquireMarkerToleratesConcurrentRelease, the rival goroutine retries with no pause, keeping a CPU core busy for 2s. The test also runs on every non-Wasm platform, but only exercises the new code on Windows. I'd add a short sleep or runtime.Gosched() in the rival loop and shorten the run, or move the test to marker_windows_test.go.
  5. Edge case, not blocking: if another waiter wrongly removes our marker as stale while we still hold it, our release's os.Remove can hit the same permission error and report it as a release failure. Treating that error like ErrNotExist in the release would fit the existing "must not remove another holder's marker" behaviour.

@amoeba

amoeba commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

Thanks @zeroshade. I've pushed fixes and am testing that we still get the failure on CI. I'll request review again when this is ready.

@amoeba

amoeba commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

@amoeba
amoeba requested a review from zeroshade November 17, 2026 02:32

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, all five addressed. Splitting on holdsOSLock is the right shape: native callers wait out a long delete-pending until their own deadline and report contention, while the Wasm path keeps the bounded grace and reports a real permission error. The doc comment now covers the grace overrun, the release tolerates a pending delete, and the concurrent-release test no longer pins a core. Nice touch confirming the new Windows tests fail without the fix. LGTM.

@amoeba
amoeba merged commit 8119c24 into columnar-tech:main Nov 17, 2026
20 checks passed
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.

2 participants