From 2e63c682b5d38b1b6b4c843146a5e193dc644483 Mon Sep 17 00:00:00 2001 From: Becky Smith Date: Wed, 12 Aug 2026 08:31:01 +0100 Subject: [PATCH] fix: Revert change to write_timestamp This was changed due to ruff rule SIM115, which flagged that a context manager should be used to write to the timestamp file. However, on windows, writing to a file while there's already an open handle to it _sometimes_ errors. The tests passed, but a user has encountered the error now. Revert the code, add the relevant noqas, and a comment so we don't inadvertently undo this again in future. --- opensafely/jobrunner/executors/volumes.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/opensafely/jobrunner/executors/volumes.py b/opensafely/jobrunner/executors/volumes.py index aef99d5b..8d053db1 100644 --- a/opensafely/jobrunner/executors/volumes.py +++ b/opensafely/jobrunner/executors/volumes.py @@ -1,5 +1,6 @@ import importlib import logging +import os import shutil import sys import tempfile @@ -57,10 +58,20 @@ def delete_volume(job): docker.delete_volume(docker_volume_name(job)) def write_timestamp(job, path, timeout=None): - with tempfile.NamedTemporaryFile() as f: + try: + # Note: writing to the temporary file while there's already a handle open to it + # is fine on unix, but sometimes errors on Windows, so we can't use a context + # manager for opening and writing to the temp file here. + f = tempfile.NamedTemporaryFile(delete=False) # noqa: SIM115 + f.close() p = Path(f.name) p.write_text(str(time.time_ns())) docker.copy_to_volume(docker_volume_name(job), p, path, timeout) + finally: + try: + os.remove(f.name) + except Exception: # noqa: S110 (ruff wants us to log this) + pass def read_timestamp(job, path, timeout=None): return docker.read_timestamp(docker_volume_name(job), path, timeout)