From 0819f5c0fd3c608e3b2775d4fc5237f6dca47bee Mon Sep 17 00:00:00 2001 From: GavinnnK Date: Fri, 7 Aug 2026 16:34:11 -0400 Subject: [PATCH] common: clean up failed atomic writes (#38552) * common: clean up failed atomic writes * common: remove atomic write regression test --------- Co-authored-by: GavinnnK <239280171+GavinnnK@users.noreply.github.com> Co-authored-by: Adeeb Shihadeh --- openpilot/common/utils.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/openpilot/common/utils.py b/openpilot/common/utils.py index 22d024fca7..a73d2b8226 100644 --- a/openpilot/common/utils.py +++ b/openpilot/common/utils.py @@ -108,10 +108,14 @@ def atomic_write(path: str, mode: str = 'w', buffering: int = -1, encoding: str if not overwrite and os.path.exists(path): 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_file = tempfile.NamedTemporaryFile(mode=mode, buffering=buffering, encoding=encoding, newline=newline, dir=dir_name, delete=False) + try: + with tmp_file: + yield tmp_file + os.replace(tmp_file.name, path) + finally: + with contextlib.suppress(FileNotFoundError): + os.unlink(tmp_file.name) def get_upload_stream(filepath: str, should_compress: bool) -> tuple[io.BufferedIOBase, int]: