Repository navigation
fix: report fsync and close errors from writePrivate - #90
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
state.writePrivatenow syncs and closes the temporary file before the rename, and returns a failed fsync or close assys.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.EINTRcounts as success and is not retried.EINTRis retried.state.write_ops, so tests can inject failures.Tests in
src/state_test.zigcover a failed fsync, a failed close, anEINTRclose and anEINTRfsync retry.