Atomic audio save temp rename
Skill kjuhwa/skills-hub/skills/python/atomic-audio-save-temp-rename
Self-correcting knowledge corpus for Claude Code — 9 stable shape clusters, bias-correction pipeline baked into contribution flow. 47 papers, 45 techniques, 1.1k skills.
npx -y skills add kjuhwa/skills-hub --skill atomic-audio-save-temp-renameAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
Write audio (or any binary) atomically by staging to a sibling .tmp file and os.replace-ing it into place on success.
SKILL.md
2.4 KB, 447 tokens by cl100k_base, as published. Nobody here has run it
Atomic audio save (temp + rename)
When to use
Any process that writes user-visible output files (generated audio, rendered images, exported bundles) where a crash mid-write would leave a corrupt half-file that the app then tries to play / load on next startup.
Steps
- Accept the final
path. Computetemp_path = f"{path}.tmp"in the same directory — same-volume guaranteesos.replaceis atomic. Path(path).parent.mkdir(parents=True, exist_ok=True)so downstream writes never fail on a missing data subdirectory.- Write to
temp_path. Forsoundfile.write, passformat='WAV'(or whatever container) explicitly because the.tmpextension does not match any recognized format and the default sniffer will raise. os.replace(temp_path, path). On POSIX and Win32 this is atomic — either old or new wins, never a truncated file.- Wrap the whole thing in try/except. On exception, attempt to unlink the
.tmp(best-efforttry/except: pass) and re-raise as a clearly-labeledOSError("Failed to save audio to ...")with the original as__cause__. - Return nothing / the final path. Never return from inside the try block before the
os.replace.
Counter / Caveats
os.renameis NOT atomic on Windows if the destination exists;os.replaceis. Always preferos.replace.- The "same filesystem" requirement is the common pitfall —
/tmpis frequently a separate volume. Keep the.tmpsibling. - If the caller is interrupted after the write but before the replace, you'll leave an orphan
.tmp. A startup-time cleanup ("delete .tmp files older than N minutes in data dirs") is a reasonable sweep if you care. - For large files, consider
fsync(temp_fd)before rename to guarantee durability —os.replaceis atomic but the new content may still be in the page cache until fsync.
Source references: backend/utils/audio.py (save_audio).
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.