Repository navigation
Leave no partly written multi-segment record after an error - #673
Conversation
MultiRecord.wrsamp already checked the segments' lengths before writing, but wrote the main header and earlier segments before finding that a later segment could not be written. Write the files into a temporary directory in write_dir and move them into place once every segment has been written. Before writing, check that each segment has the signal to write, and samps_per_frame for expanded signals, so that these give a ValueError naming the segment rather than a TypeError. Pass write_dir when opening an archive, so that a relative wfdb_archive path is relative to it, as in Record.wrsamp. Fixes #557.
tompollard
left a comment
There was a problem hiding this comment.
Reviewed commit 259147c. I found two issues in the staged-file commit step (src/wfdb/io/record.py, lines 1220-1225):
-
Preserve existing file permissions when overwriting a record.
os.replacegives the destination the staged file's permissions. With umask022, I wrote a record, changed041s01.datto mode0600, then wrote it again: this PR changed the file to0644, while the base version kept it at0600. That can expose data in a shared directory. Please preserve each existing file's mode on replacement. -
Handle a failure during the final moves. The files are replaced one by one without rollback. When I made the second
os.replaceraiseOSError, the first moved file remained in the output directory (041s01.hea), leaving a partial record. Replacing an existing record can likewise leave old and new files mixed. Please handle this case, or narrow the all-or-nothing guarantee and document the limitation.
All 18 focused offline multi-record tests pass. CI has no failures so far, with several jobs still running. I would address these before merging.
Moving the written files into place gave a replaced file the new file's permissions, and a failure partway through left the files moved so far. Give each new file the permissions of the file it replaces, move replaced files aside rather than deleting them, and if a move fails, remove the files already moved and restore the replaced ones before raising the error.
|
Thanks, both fixed in c34985d, in a new helper,
Two limits remain:
All offline tests and ruff pass. |
tompollard
left a comment
There was a problem hiding this comment.
Re-reviewed commit c34985d. Both findings from my earlier review are resolved: overwriting an existing record now preserves its file modes, and a simulated failure during the final moves restores the previous files. The 21 focused offline multi-record tests pass. I found no new blocker. Docs and style checks pass; the test matrix is still running.
One wording caveat: the rollback covers handled write errors. Sequential moves cannot provide a single atomic view to concurrent readers or recover from a process crash, so the all-or-nothing claim should be read in that narrower sense.
|
Agreed, thanks. The guarantee is narrower than "all or nothing", so in 1958a92 I changed the wording to match:
No code changes in this commit. The docs build and ruff pass. |
Fixes #557.
The "good initial step" in the issue, calling
_check_segment_cohesionbefore writing, is already onmain(3880107, in 5.0.0). This covers the rest of what the issue asks for, and what the checks miss.A later segment that fails no longer leaves a partial record. The cohesion check only covers lengths and
fs. Anything thatRecord.wrsamprejects in a later segment (an invalid format, an expanded signal of the wrong length, and so on) was found only after the main header and the earlier segments had been written:041smain: files left after the error041s.hea,041s01.hea,041s01.dat,041s02.hea041s.hea,041s01.hea,041s01.datMultiRecord.wrsampnow writes into a temporary folder insidewrite_dir, and moves the files into place (withos.replace, on the same file system) only once every segment has been written. Writing to a.wfdbarchive opened from a path was already all-or-nothing, since the archive is discarded on an error. Files that are replaced keep their permissions, and if one of the moves fails, the files already moved are removed and the replaced files are restored. A crash partway through the moves can't be undone.Clear errors for missing signals. As the issue suggests, each segment is checked before writing for the signal to write (
d_signalorp_signal, ore_d_signalore_p_signalwithexpanded=True) and, for expanded signals,samps_per_frame. These gaveTypeError: object of type 'NoneType' has no len()oriteration over a 0-d array, and now give aValueErrorsuch as "Segment 1 (041s02) has no samps_per_frame, which is needed to write its expanded signal". Empty segments and the layout header of a variable layout record are skipped, as they have no signal files.Archive paths relative to
write_dir.MultiRecord.wrsamp(write_dir="out", wfdb_archive="041s.wfdb")wrote the archive to the current directory, becausewrite_dirwasn't passed when opening it. It now goes inout/, as withRecord.wrsamp.Testing
samps_per_frameand missing signals give the new errors; writing to the current directory leaves no temporary folder; a relative archive path is relative towrite_dir; overwriting keeps file permissions; and a failed move leaves an empty folder empty and restores a record it was replacing. All but the current-directory test fail onmain.pytest: all offline and network tests pass. Ruff and the docs build pass.@tompollard 🟥 red 🦥 sloth · aspen