Skip to content

volume: close the file after a successful rawiv write - #663

Merged
transfix merged 1 commit into
masterfrom
fix/rawiv-close-on-write
Oct 18, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/rawiv-close-on-write

Conversation

@transfix

Copy link
Copy Markdown
Owner

rawiv_io::writeVolumeFile never closed its file after a successful write. Its only fclose(output) on the success path sat inside an #if 0 block, so every .rawiv write leaked the FILE*.

Effects

  • Unflushed data. The writer fseeks before each scanline, which flushes the stdio buffer, so only the last scanline stayed unflushed. A read in the same process (or any reader before exit) saw zeros there.
  • Windows file lock. On Windows the file stayed locked until the process exited. This is why pycvc's test_volume_rawiv_roundtrip failed on Windows, with a PermissionError while TemporaryDirectory cleaned up.
  • The old test missed it. The existing RawivRoundTrip passed because expect_volume_equal skips the far corner, citing "boundary-plane quantization on a full vertex-vs-cell round-trip". The missing close may be what that comment was seeing.

Fix

  • Close the file after the (still disabled) min/max block, and check fclose's result, since a late flush error only surfaces there.
  • Drop the dead fclose inside #if 0, so re-enabling that block can't double-close.

The other volume writers (.rawv, .mrc, spider, cvcraw) close properly. Their #if 0 blocks contain no closes.

Test

volume_io_test gains RawivWriteFlushesEveryVoxelAndClosesTheFile. It reads back every voxel, the far corner included, then removes the file straight away.

  • Before the fix, on Windows MSVC Release, it failed on all five voxels of the last scanline, and the remove failed with "being used by another process".
  • After the fix, it passes, as do the rest of volume_io_test (26/26 on a quiet machine) and volume_io_extra_test (54/54).
  • pycvc: test_pycvc.py passes 15/15, test_volume_rawiv_roundtrip included. A probe shows the file unlocked right after save().

Unrelated, pre-existing

Run as one process on a loaded machine, volume_io_test sometimes segfaults in the test after CvcRawRoundTrip, whose background build_hierarchy threads appear to outlive the test's app. It crashed 5/5 on this branch and 5/5 on an unmodified build of the main checkout under the same load, and it passes when run quietly. CI runs each gtest case in its own process (cvc_discover_tests), so it doesn't show up there. Tracked separately.

rawiv_io::writeVolumeFile's only fclose on the success path sat inside an
#if 0 block, so every successful .rawiv write leaked its FILE*. The last
scanline stayed in the stdio buffer (the per-scanline fseek flushes the
others), so a read in the same process saw zeros there, and on Windows
the file stayed locked until the process exited. pycvc's
test_volume_rawiv_roundtrip failed on Windows for this reason
(PermissionError deleting its temp directory).

Close the file after the (still disabled) min/max block, checking the
result since a late flush error surfaces at fclose, and drop the dead
close inside #if 0 so re-enabling it can't double-close.

volume_io_test gains RawivWriteFlushesEveryVoxelAndClosesTheFile: it
compares every voxel, the far corner included, and removes the file
straight after the read. Before the fix it failed on the five voxels of
the last scanline and on the remove. expect_volume_equal skips the far
corner, citing "boundary-plane quantization"; this missing close may be
what that comment saw.
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