From 49d5bc171f6296ac23a285a639d2b21b37ae37eb 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. https://github.com/opensafely-core/opensafely-cli/commit/12eadba5a2087b6fd2ebaf5642cc157427d98559 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. The previous workaround was a bit odd, and involved opening a temp file with delete=False, immediately closing it, writing to it and then manually cleaning it up afterwards. A cleaner and more ruff-friendly way is to use a temp directory instead, and write to a file inside it without opening the file. --- opensafely/jobrunner/executors/volumes.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/opensafely/jobrunner/executors/volumes.py b/opensafely/jobrunner/executors/volumes.py index aef99d5b..7e503dda 100644 --- a/opensafely/jobrunner/executors/volumes.py +++ b/opensafely/jobrunner/executors/volumes.py @@ -57,10 +57,18 @@ def delete_volume(job): docker.delete_volume(docker_volume_name(job)) def write_timestamp(job, path, timeout=None): - with tempfile.NamedTemporaryFile() as f: - p = Path(f.name) + # Note: writing to a temporary file while there's already a handle open to it + # is fine on unix, but sometimes errors on Windows, so we use a temp directory + # here instead of opening/writing to a temp file directly. + with tempfile.TemporaryDirectory() as tmpdir: + p = Path(tmpdir) / "timestamp" p.write_text(str(time.time_ns())) - docker.copy_to_volume(docker_volume_name(job), p, path, timeout) + docker.copy_to_volume( + docker_volume_name(job), + p, + path, + timeout, + ) def read_timestamp(job, path, timeout=None): return docker.read_timestamp(docker_volume_name(job), path, timeout)