Repository navigation
Don't truncate existing file when save fails - #521
Merged
Merged
Conversation
`save` opened the target with 'wb', truncating it before the new contents were written. A failed write (disk full, file size limit, process killed) left an existing session or structure file empty. Write to a temporary file in the same directory and os.replace() it over the target, keeping the file mode. Symlinks, non-regular files, read-only files and non-writable directories fall back to the previous direct write. gzip output is now compressed in memory like bz2, which drops the original filename from the gzip header. Fixes #520 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
JarrettSJohnson
added a commit
that referenced
this pull request
Oct 7, 2026
get_gltf writes the file itself, so it bypassed the atomic write that save gained in #521 and a failed write could still truncate an existing .gltf file. Use exporting._write_file_atomic, and cover glb/gltf in the save failure test (requires_capability now returns a pytest mark, so it also works in pytest.param). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #520
Problem
saveopened the target with'wb', which truncates an existing file before the new contents are written. Any failure after the open (disk full, file size limit, process killed) left the previous file empty. For.pse/.pze/.pse.gzsessions, that means losing scenes, views, selections and coloring. I reproduced this onmasterfor all three session formats withulimit -f 0.Fix
A new helper,
_write_file_atomic, writes to a temporary file in the same directory and thenos.replace()s it over the target:shutil.copymode). The temp file is created withopen(..., 'xb'), so new files still respect the umask./dev/stdout), read-only files and writable files in non-writable directories fall back to the previous direct write. Read-only files are still refused instead of being silently replaced.gzip.compress, as bz2 already was. Side effect: the gzip header no longer stores the original filename, which only matters togunzip -N.Not covered: exporters that write the file themselves (e.g.
png,mmod,pkl). There is nofsync, so power loss is out of scope, as in the issue.Testing
testing/tests/api/test_exporting.py:ENOSPCpartway through the write leaves.pse/.pse.gz/.pdbunchanged, with no temp files left behind. These fail onmaster.ulimit -f 0steps,.pse,.pse.gzand.pzestay byte-for-byte the same. Compressed sessions still load.tests/api/exporting.pyandtests/api/importing.pyviatesting.py: 141 tests, all pass excepttestglTF, which errors locally becausecollada2gltfis not installed.🤖 Generated with Claude Code