Repository navigation
fsync the temporary .env file before replacing it - #754
Open
boriskozak wants to merge 2 commits into
Open
boriskozak wants to merge 2 commits into
boriskozak wants to merge 2 commits into
Conversation
rewrite() wrote the new contents to a temporary file and renamed it over the target without fsync, so a power loss shortly after set_key or unset_key returned could leave behind an empty or garbage .env. Flush and fsync the temporary file while it is still open, and best-effort fsync the containing directory on POSIX so the rename itself is durable. Directory fsync failures are ignored. A flush or fsync failure still discards the temporary file and reports the original error. Fixes theskumar#713
…h typing The directory-fsync tests assert on os.fsync calls, but _fsync_directory returns early on non-POSIX platforms, so the calls never happen on Windows. Skip them there. Annotate the patcher in the flush/fsync failure test as AbstractContextManager[Any]: the two mock.patch call shapes return incompatible _patch generics and mypy 3.13 rejects the reassignment.
This branch has not been deployed
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.
Problem
set_keyandunset_keyrewrite the.envfile throughrewrite(): they write a temporary file next to the target, thenos.replaceit over the target. The temporary file is closed (which flushes Python's buffer to the OS) and then renamed, with nofsync. If the machine loses power or the kernel crashes shortly afterset_keyreturns, the rename can reach the disk before the file's data does, leaving an empty or garbage.envbehind instead of either the old or the new contents.Fix
In
rewrite()(src/dotenv/main.py):os.fsyncit, so the new contents are on disk before the rename.os.replace, best-effort fsync the containing directory on POSIX so the rename itself is durable. Failures are ignored: the contents are already safe, and some filesystems reject directory fsync.close(), so that path is folded in too).Testing
os.fsync/os.replaceand recorded call order;os.replaceran with no precedingfsyncfor bothset_keyandunset_key. After the fix,fsyncruns beforereplacefor both.set_key/unset_key, directory-fsync failures ignored (both theos.openandEINVALpaths), and flush/fsync failures leaving the original file untouched with no temp file behind.tests/test_main.py: 161 passed, 3 skipped. Ruff check and format clean.Fixes #713