Skip to content

[Bug]: Workflow checkpoint fails permanently on Windows when state.json is held open (WinError 5 in os.replace) #5520

Backlog

Description

@mcowdell-westmar

Bug Description

On Windows, RunState._atomic_write_json (src/specify_cli/workflows/engine.py) fails with PermissionError: [WinError 5] Access is denied when any other handle holds the target state.json open at the moment of os.replace(tmp, path). RunState.save() converts this into CheckpointError, sets _checkpoint_failed, and the run is left with status running. specify workflow resume then refuses the run, so a transient lock from a reader, indexer, antivirus scanner or second process ends the run permanently.

The code is unchanged on main (a4d5712): _atomic_write_json calls os.replace once, with no retry.

Steps to Reproduce

On Windows, with specify-cli 1.4.0 installed:

import tempfile
from pathlib import Path
from specify_cli.workflows.engine import RunState

d = Path(tempfile.mkdtemp())
target = d / "state.json"
target.write_text("{}", encoding="utf-8")

holder = open(target, "r", encoding="utf-8")   # a second handle on the target
RunState._atomic_write_json(target, {"status": "running"})   # raises PermissionError

Closing holder first makes the same call succeed.

In a real run the second handle came from another process reading the run directory while a long workflow was checkpointing; the failure ended two runs.

Expected Behavior

A transient sharing violation on Windows does not end the run. The write is retried for a short bounded period and succeeds once the other handle is released; a persistent error is still raised.

Actual Behavior

The first PermissionError is raised, save() raises CheckpointError, the run stays running, and specify workflow resume refuses it.

Specify CLI Version

1.4.0

AI Agent

Claude Code

Operating System

Windows 11 Pro 10.0.26200

Python Version

Python 3.12.13

Error Logs

Traceback (most recent call last):
  File "<TEMP>\repro.py", line 16, in <module>
    RunState._atomic_write_json(target, {"status": "running"})
  File "<site-packages>\specify_cli\workflows\engine.py", line 886, in _atomic_write_json
    os.replace(tmp, path)
PermissionError: [WinError 5] Access is denied: '<TEMP>\\tmpjqtrj7kv\\.state.json.dj33mh0b.tmp' -> '<TEMP>\\tmpjqtrj7kv\\state.json'

Additional Context

Proposed fix, which I can open as a PR: retry os.replace in _atomic_write_json on PermissionError only, a fixed small number of times with a short backoff, and re-raise the last error unchanged. Other exceptions keep their current path. A regression test that simulates the sharing violation by patching os.replace fails on main and passes with the change.

AI Disclosure

Diagnosed, reproduced and written with Claude Code using Claude Opus 5.5, running autonomously at the direction of the account holder, who decided to report it upstream; effort setting not recorded. Extent: the reproduction script, the traceback above (produced by running it) and the text of this issue were generated by the agent.

Activity

  1. added
    triage-must-have
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage-must-have

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions