extract: report a failing close() of a destination file as a warning - #10332
Open
ThomasWaldmann wants to merge 1 commit into
Open
extract: report a failing close() of a destination file as a warning#10332ThomasWaldmann wants to merge 1 commit into
ThomasWaldmann wants to merge 1 commit into
Conversation
extract_item writes the file content through a buffered writer, so the
content of a small file (up to the buffer size, usually 4 KiB) only reaches
the OS when the buffer gets flushed: at the truncate()/flush() after the
chunk loop - and again at close() if that flush failed, because the data is
still buffered then. The first failure happens inside a backup_io block and
thus becomes a BackupOSError, i.e. a warning for that file. The second one
came from the implicit close of "with fd:", outside of any backup_io block,
so it escaped as a plain OSError and aborted the whole extraction with a
traceback ("Local Exception", rc 2). On a full disk (or quota, or an I/O
error) that meant: warnings for the big files, then an abort at the first
small file - the "warn and continue" behaviour of extract did not survive it.
Close the file explicitly: under backup_io("close") when everything else
succeeded (close(2) itself can fail, too, e.g. on NFS), and with a failing
close ignored when an exception is already in flight - the file's failure
is reported by that exception, and a repository error or a KeyboardInterrupt
must not be turned into a per-file warning.
Add tests for both cases (the raw file's writes fail like on a full disk /
its close fails), using a buffered writer like open() gives us.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #10332 +/- ##
=======================================
Coverage 87.74% 87.75%
=======================================
Files 103 103
Lines 18817 18823 +6
Branches 2905 2905
=======================================
+ Hits 16511 16518 +7
Misses 1602 1602
+ Partials 704 703 -1 ☔ View full report in Codecov by Harness. |
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.
What
extract_item()writes the file content through a buffered writer, so the content of a small file (up to the buffer size, usually 4 KiB) only reaches the OS when the buffer gets flushed: at thetruncate()/flush()after the chunk loop - and again atclose()if that flush failed, because the data is still buffered then.The first failure happens inside a
backup_ioblock and becomes aBackupOSError, i.e. a warning for that file. The second one came from the implicit close ofwith fd:, outside of anybackup_ioblock, so it escaped as a plainOSErrorand aborted the whole extraction with a traceback ("Local Exception", rc 2). Reproduced with a file size limit (ulimit -f), a full disk behaves the same with ENOSPC:Big files are not affected: a chunk larger than the buffer is written directly, and after a failed direct write nothing is left in the buffer. So the abort needs a file whose creation succeeds while its data write fails, i.e. it depends on how the filesystem behaves at the boundary. Verified with a real full disk (a ramdisk, no error injection), extracting an archive of 20000 files of 4 KiB:
backup_io, so there master just warned as well. ext4 and tmpfs create inodes without needing data blocks, so they should behave like APFS (not verified).How
Close the file explicitly:
backup_io("close")when everything else succeeded (close(2)itself can fail, e.g. on NFS), so a failure there is a warning for that file, like any other IO error on it;KeyboardInterruptmust not be turned into a per-file warning.Tests
Two tests in
extract_cmd_test.pypatchopen()inborg.archiveto return a buffered writer (likeopen()does) over a raw file whose writes fail like on a full disk, resp. whose close fails: every file gets its warning and the run ends with the specific exit code. Without the fix, the first one aborts with theOSErrorescaping from the close.Found while auditing
borg extractfor memory growth (#10331 is the other finding from that).🤖 Generated with Claude Code