Skip to content

fix(workflows): prevent concurrent resume of the same workflow run - #5581

Open
Magshifter wants to merge 1 commit into
github:mainfrom
Magshifter:fix/sk-01-resume-run-lock
Open

Magshifter wants to merge 1 commit into
github:mainfrom
Magshifter:fix/sk-01-resume-run-lock

Conversation

@Magshifter

Copy link
Copy Markdown

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:

  • Introduces exclusive per-run ownership using
    the existing inter-process locking mechanism.
  • Acquires ownership before loading the run state
    and retains it throughout resume execution.
  • Immediately rejects competing resume attempts.
  • Protects legacy CLI metadata writes under the
    same ownership boundary.
  • Preserves concurrent execution of different runs
    and existing blocking-lock behavior.
    No new external dependencies are introduced.

Testing

Set up this checkout's .venv and editable install using the local development guide. Run commands from the checkout root.

  • Tested locally with .venv/bin/specify --help (Windows PowerShell: .\.venv\Scripts\specify.exe --help)
  • Ran relevant existing tests with .venv/bin/python -m pytest tests -q (Windows PowerShell: .\.venv\Scripts\python.exe -m pytest tests -q); use affected test paths for focused checks and LC_ALL=en_US.UTF-8 on macOS
  • Tested with a sample project (if applicable)

The specify --help command and a sample-project run are not in the retained evidence, so those boxes stay open. Retained pytest sessions identify the interpreter C:\laragon\repos\spec-kit\.venv\Scripts\python.exe on Python 3.14.7. The exact pytest argument list is not retained in those logs. Ruff's retained output is All checks passed!; its invocation line is not in the log.

Verified on native Windows/amd64.

  • Baseline f61648d: regression tests reproduce
    concurrent ownership of the same workflow run.
  • Corrected revision 3d70d11:
    15 passed, 1 skipped.
  • Workflow tests: 905 passed, 5 skipped.
  • Ruff: PASS.
  • Full Windows suite:
    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

  • I did not use AI assistance for this contribution
  • I did use AI assistance (fill in the disclosure below)

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.

  • Planning and analysis: ChatGPT (GPT-6.2).
  • Implementation and regression tests: Cursor Agent
    (Grok 4.7, High reasoning effort, autonomous).
  • Independent review and verification: Grok Bot.
  • Product Owner and contributor: Magshifter.
    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.

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)
@Magshifter
Magshifter requested a review from mnriem as a code owner November 25, 2026 23:36
Copilot AI adaptive review requested due to automatic review settings November 25, 2026 23:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants