set_key and unset_key don't fsync before replacing the .env file
維護者通常 2 天內回覆
評估
研究方向
Start in src/dotenv/main.py at rewrite(), then trace its callers from set_key, unset_key, and the dotenv set/unset commands. Find the existing tests covering rewrite or key updates and add coverage that records fsync and os.replace ordering. Done means the temporary file is durable before replacement, with any directory-durability behavior covered consistently across supported platforms.
由索引模型根據 Issue 內容生成。
描述
set_key and unset_key rewrite the .env file through rewrite() (src/dotenv/main.py): they write a temporary file next to the target, then os.replace it over the target. That makes the swap atomic for other processes, but nothing forces the new contents to disk before the rename. The temporary file is closed (which flushes Python's buffer to the OS) and then renamed, with no fsync.
What can go wrong
If the machine loses power or the kernel crashes shortly after set_key returns, the rename can reach the disk before the file's data does. After a reboot, .env can then be empty (zero length) or contain garbage, instead of holding either the old or the new contents. This applies to set_key, unset_key, and the dotenv set / dotenv unset commands.
How likely this is depends on the filesystem. ext4 with its default auto_da_alloc option flushes data before a rename that replaces an existing file, which covers this exact pattern. Other filesystems (XFS, and ext4 mounted with noauto_da_alloc) make no such promise, and a .env that has never existed before isn't covered by that heuristic at all. I haven't reproduced data loss; this is based on the documented semantics of rename(2) and fsync(2), and is the reason other write-then-rename implementations call fsync.
Since .env files often hold credentials, losing one silently is a bad outcome.
Proposed change
In rewrite(), before os.replace:
dest.flush()
os.fsync(dest.fileno())
This has to happen while the temporary file is still open, i.e. inside the with temp_file as dest: block, after the caller's writes have finished.
Optionally, after os.replace on POSIX, fsync the containing directory so the rename itself is durable:
if os.name == "posix":
dir_fd = os.open(os.path.dirname(os.path.abspath(path)), os.O_RDONLY)
try:
os.fsync(dir_fd)
finally:
os.close(dir_fd)
Directory fsync isn't available on Windows, and some filesystems reject it with EINVAL. Since the file contents are already safe at that point, an OSError from it could reasonably be ignored.
Cost
.env files are small and set_key is not called in hot loops, so one or two fsync calls per write should not be noticeable. A test can check that os.fsync is called on the temporary file before os.replace, for example by patching both in dotenv.main and recording the call order.
Out of scope
Concurrent writers (two set_key calls at once can lose one update) and file-replacement side effects such as single-file Docker bind mounts are separate problems and not addressed here.
- 主要語言
- Python
- 星號
- 8.9k
- 分支
- 617
- 平均合併
- 11 小時 19 分鐘
- 30 天內合併 PR
- 3
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
theskumar/python-dotenv 的其他 Issue
-
An empty value ignores ${name:-default}可能已有人在做 @SashaMIT 於 26 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 75/100
theskumar/python-dotenv#715 ·
維護者通常 2 天內回覆
-
難度 5/5 一週以上 新手友好度 28/100
theskumar/python-dotenv#693 · 1 則留言 ·
維護者通常 2 天內回覆
-
python-dotenv installation without cli masks python-dotenv cli可能已有人在做 @gyanu2507 於 65 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 55/100
theskumar/python-dotenv#683 · 4 則留言 ·
維護者通常 2 天內回覆
-
Feature Request: Automatically copy `default.env` to `.env`可能已有人在做 @dradetsky 於 174 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 55/100
theskumar/python-dotenv#644 · 2 則留言 ·
維護者通常 2 天內回覆
-
Add `strict` parameter to `load_dotenv()` and `dotenv_values()` for fail-fast behavior可能已有人在做 @AhsanSheraz 於 226 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 68/100
theskumar/python-dotenv#631 · 3 則留言 · 1 個 reaction ·
維護者通常 2 天內回覆
查看 theskumar/python-dotenv 的全部 Issue
相似的 Issue
-
lesson-submission pending-review
難度 2/5 1-3 小時 新手友好度 72/100
Ikalus1988/MisakaNet#3956 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
aws-samples/sample-per-model-bedrock#574 ·
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 70/100
521xueweihan/HelloGitHub#4133 ·
-
plot_components fails for logistic models with minmax scaling and no floor可能已有人在做 @RaphaelFakhri 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 4 天內回覆
-
Update migen未關閉
難度 2/5 1-3 小時 新手友好度 70/100
YosysHQ/oss-cad-suite-build#220 ·