From 268769fc45b8ba1c0fb4127bb28c8d79c8c15948 Mon Sep 17 00:00:00 2001 From: Alex Schapiro Date: Wed, 26 Aug 2026 23:04:35 +0000 Subject: [PATCH] fix(cli): report a leftover temporary secret file instead of hiding it --- strix/utils/secret_files.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/strix/utils/secret_files.py b/strix/utils/secret_files.py index 5ba0fc1c..023aa87f 100644 --- a/strix/utils/secret_files.py +++ b/strix/utils/secret_files.py @@ -23,14 +23,26 @@ def write_secret_text(path: Path, text: str) -> None: try: with os.fdopen(fd, "w", encoding="utf-8") as handle: handle.write(text) - except BaseException: - with contextlib.suppress(OSError): - tmp.unlink() + except BaseException as exc: + _cleanup_tmp(tmp, exc) raise try: tmp.replace(path) - except BaseException: - with contextlib.suppress(OSError): - tmp.unlink() + except BaseException as exc: + _cleanup_tmp(tmp, exc) raise + + +def _cleanup_tmp(tmp: Path, cause: BaseException) -> None: + """Delete the temporary secret file. A failed delete must not stay silent.""" + try: + tmp.unlink() + except FileNotFoundError: + pass + except OSError: + message = ( + f"could not store the secret, and the temporary file {tmp} " + f"still holds it. Delete the file manually." + ) + raise OSError(message) from cause