Repository navigation
fix(workflows): prevent concurrent resume of the same workflow run - #5581
Open
Magshifter wants to merge 1 commit into
Open
Magshifter wants to merge 1 commit into
Magshifter wants to merge 1 commit into
Conversation
Concurrent resume could both observe a paused run and overwrite its checkpoint. Reuse the project lock for the whole resume, including the CLI legacy metadata write. Assisted-by: Cursor Agent (model: Grok 4.7, high reasoning effort, autonomous)
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Copied or stale contexts can bypass the run lock and permit concurrent resumes, leaving a critical flaw in the ownership protection.
8 open issues
Copied contexts can bypass the run lock · New Return JSON when a resume loses the lock · New Bound lock filenames for long run IDs · New Clear stale execution state before lock acquisition · New Restore stdin after tests instead of replacing it globally · New Read worker results only after they are fully written · New Assert different runs overlap before releasing the test workers · New Move CLI-specific resume assertions to the CLI test suite · New
What changed in this PR
Adds per-run ownership to Spec Kit’s workflow resume path to prevent duplicate step execution and checkpoint overwrites from competing processes.
Changes:
- Wraps engine resumes and CLI legacy metadata writes in the same ownership boundary.
- Adds nonblocking acquisition while preserving existing blocking-lock behavior.
- Adds regression tests for contention, distinct runs, and lock release.
AI-assisted review for the requester by GitHub Copilot (model unknown; automated analysis and review drafting). PR-reported revision: 42629bd.
| File | Description |
|---|---|
tests/workflows/test_resume_run_lock.py |
Adds engine and CLI ownership regression tests. |
tests/test_shared_infra_lock.py |
Tests immediate contention rejection and lock-file persistence. |
src/specify_cli/workflows/engine.py |
Introduces per-run ownership around resume execution. |
src/specify_cli/workflows/command_resume.py |
Locks state loading, legacy metadata updates, and execution together. |
src/specify_cli/shared_infra.py |
Adds optional nonblocking inter-process lock acquisition. |
Review effort: Max (adaptive)
Comment on lines
+1081
to
+1083
| if key in _resume_run_owners.get(): | ||
| yield | ||
| return |
Comment on lines
+152
to
154
| except OSError as exc: | ||
| err.print(f"[red]Resume failed:[/red] {cli._escape_markup(str(exc))}") | ||
| raise cli.typer.Exit(1) |
|
|
||
| RunState._validate_run_id(run_id) | ||
| root = Path(project_root).resolve() | ||
| lock_name = f"workflow-run-{run_id}.lock" |
Comment on lines
+1297
to
1305
| with _resume_run_lock(self.project_root, run_id): | ||
| return self._resume_owned(run_id, inputs) | ||
|
|
||
| def _resume_owned( | ||
| self, | ||
| run_id: str, | ||
| inputs: dict[str, Any] | None, | ||
| ) -> RunState: | ||
| self._execution_state.set(None) |
Comment on lines
+77
to
+80
| sys.stdin.isatty = lambda: False # type: ignore[method-assign] | ||
| state = WorkflowEngine(project).execute( | ||
| _definition(log, release), run_id=run_id | ||
| ) |
Comment on lines
+184
to
+186
| or any( | ||
| path.exists() and not _read_result(path).get("ok") for path in results | ||
| ) |
Comment on lines
+240
to
+241
| release.write_text("go", encoding="utf-8") | ||
| finished = [_finish(proc) for proc in procs] |
Comment on lines
+248
to
+249
| def test_cli_legacy_second_resume_does_not_write(tmp_path: Path) -> None: | ||
| """The legacy metadata save is inside the same ownership boundary.""" |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Description
Fix a race condition that allows multiple processes
to resume the same workflow run concurrently.
Previously, two processes could independently load
a paused run, pass the status check, execute the
same remaining steps, and overwrite each other's
checkpoints. Atomic state persistence alone did not
prevent duplicate execution.
This change:
the existing inter-process locking mechanism.
and retains it throughout resume execution.
same ownership boundary.
and existing blocking-lock behavior.
No new external dependencies are introduced.
Testing
Set up this checkout's
.venvand editable install using the local development guide. Run commands from the checkout root..venv/bin/specify --help(Windows PowerShell:.\.venv\Scripts\specify.exe --help).venv/bin/python -m pytest tests -q(Windows PowerShell:.\.venv\Scripts\python.exe -m pytest tests -q); use affected test paths for focused checks andLC_ALL=en_US.UTF-8on macOSThe
specify --helpcommand and a sample-project run are not in the retained evidence, so those boxes stay open. Retained pytest sessions identify the interpreterC:\laragon\repos\spec-kit\.venv\Scripts\python.exeon Python 3.14.7. The exact pytest argument list is not retained in those logs. Ruff's retained output isAll checks passed!; its invocation line is not in the log.Verified on native Windows/amd64.
concurrent ownership of the same workflow run.
15 passed, 1 skipped.
17,121 passed, 190 failed, 558 skipped.
All 190 failures were reproduced on the unchanged
baseline under the same Windows test environment.
No additional failures attributable to this
correction were identified.
The final publication commit 42629bd has the
identical Git tree and parent as tested revision
3d70d11. Only commit metadata was changed.
Linux and macOS runtime verification was not run.
AI Disclosure
This contribution was developed with AI assistance
under the MAG-CORE governance framework, with
human-directed planning, controlled implementation,
evidence-based testing, and independent verification.
(Grok 4.7, High reasoning effort, autonomous).
AI assistance covered defect investigation, code
generation, regression testing, independent review,
and preparation of this pull request.
Development objectives and decisions were directed
by the Product Owner.