Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

atomic_write_in_dir: ensure flush and sync for atomic writes #34527

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions common/file_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ def atomic_write_in_dir(path: str, mode: str = 'w', buffering: int = -1, encodin
raise FileExistsError(f"File '{path}' already exists. To overwrite it, set 'overwrite' to True.")

with tempfile.NamedTemporaryFile(mode=mode, buffering=buffering, encoding=encoding, newline=newline, dir=dir_name, delete=False) as tmp_file:
yield tmp_file
tmp_file_name = tmp_file.name
os.replace(tmp_file_name, path)
tmp_path = tmp_file.name
try:
yield tmp_file

tmp_file.flush()
os.fsync(tmp_file.fileno())
os.replace(tmp_path, path)
except Exception as e:
os.unlink(tmp_path)
raise e


def get_upload_stream(filepath: str, should_compress: bool) -> tuple[io.BufferedIOBase, int]:
Expand Down