Skip to content

fix: report fsync and close errors from writePrivate - #90

Merged
HaoZeke merged 2 commits into
developfrom
fix/write-private-close
Nov 25, 2026
Merged

HaoZeke merged 2 commits into
developfrom
fix/write-private-close

Conversation

@HaoZeke

@HaoZeke HaoZeke commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

state.writePrivate now syncs and closes the temporary file before the rename, and returns a failed fsync or close as sys.Error. On failure the name stays as it was and the temporary file is removed. Before, the close ran after the rename and its error was dropped, so a write that NFS reported lost at close replaced the name and returned success.

  • A close interrupted by EINTR counts as success and is not retried.
  • An fsync interrupted by EINTR is retried.
  • The calls go through state.write_ops, so tests can inject failures.

Tests in src/state_test.zig cover a failed fsync, a failed close, an EINTR close and an EINTR fsync retry.

writePrivate closed the temporary file in a defer that ran after the
rename and dropped the result of close. On a network file system (NFS)
a write the server loses can be reported by close alone, so the name
was replaced with a file whose bytes may be gone and the call returned
success. The Python tools this port follows close before os.replace and
raise on a failed close, so they never replace the name in that case.

The temporary file is synced and closed before the rename. A failed
fsync or close is returned as sys.Error with sys.last_errno set to its
errno, the name is left as it was, and the temporary file is removed.
The removal keeps that errno, so sys.describe names the failed call.
The descriptor is closed once on every path: by the close before the
rename, or by the cleanup when an earlier call fails.

close is called once and its result goes through sys.result. `EINTR`
is not retried, because Linux frees the descriptor before close returns
and a retry could close a descriptor another thread has just opened.
sys.result returns null for `EINTR` rather than an error, so that close
is not an error either. The fsync before it has flushed the bytes, so
that close has nothing left to lose.

fsync and close go through state.write_ops, replaceable in the same
builds as state.lock_ops and constant real calls in any other build.
Through state.write_ops, the cases make fsync fail with `EIO` or
`ENOSPC`, and close with `EIO`, `ENOSPC` or `EDQUOT`. Those are the
errors a network file system (NFS) reports for a lost write. Each case
runs over an existing name and over a name that does not exist yet. Each
expects the mapped error with sys.last_errno and sys.describe naming
that errno, one close of the descriptor, the descriptor count from
before the call, the existing name with its old bytes, no new name and
no temporary file. A close that reports `EINTR` expects success, one
close and the new bytes. A sync that reports `EINTR` once expects a
second sync, one close and the new bytes. A plain write expects one
close.

The close hook closes the descriptor for real before it reports its
errno, as Linux does, so a leak or a second close shows in the counts.
On develop before this series the tests fail to build, as it has no
state.write_ops.
@HaoZeke
HaoZeke merged commit 9cc29f1 into develop Nov 25, 2026
8 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.

1 participant