Skip to content

fix(jobs): make completed->completed transition idempotent to prevent race errors - #1068

Merged
zmanian merged 2 commits into
stagingfrom
fix/731-idempotent-job-completion
Mar 16, 2026
Merged

zmanian merged 2 commits into
stagingfrom
fix/731-idempotent-job-completion

Conversation

@zmanian

@zmanian zmanian commented Mar 12, 2026

Copy link
Copy Markdown
Collaborator

Summary

Supersedes #731. Fixes the race condition where jobs error out despite completing successfully.

  • Root cause: Both execution_loop and the worker wrapper in execute() race to call mark_completed(). The state machine rejects the second Completed -> Completed transition, causing the job to error out.
  • Fix: Narrowly allow only the Completed -> Completed self-transition as idempotent -- early return with tracing::debug! log, no duplicate history entry. All other self-transitions (Pending -> Pending, InProgress -> InProgress, etc.) remain rejected to preserve state machine strictness.
  • Updated test: test_mark_completed_twice_returns_error renamed to test_mark_completed_twice_is_idempotent and updated to expect Ok.
  • New regression tests: test_completed_to_completed_is_idempotent (state-level) and test_other_self_transitions_still_rejected (ensures narrow scope).

Addresses @zmanian's review feedback on #731:

  • Scoped to only Completed -> Completed (not all self-transitions)
  • Added tracing::debug! in the idempotent early-return path
  • Updated and added regression tests

Test plan

  • cargo check passes
  • cargo test --lib context -- all 79 tests pass (2 new)
  • cargo clippy --all --tests -- zero warnings
  • Manual: trigger concurrent mark_completed calls to verify no error

Closes #731

Generated with Claude Code

@github-actions github-actions Bot added scope: worker Container worker size: M 50-199 changed lines risk: medium Business logic, config, or moderate-risk modules contributor: core 20+ merged PRs labels Mar 12, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a critical fix for a race condition within the job execution system. Previously, jobs that completed successfully could still report an error if both the execution loop and the worker wrapper attempted to mark them as completed concurrently. By making the Completed -> Completed state transition idempotent, the system now gracefully handles these concurrent calls, preventing erroneous job failures and significantly improving the robustness and reliability of job processing.

Highlights

  • Race Condition Fix: Resolved a race condition where jobs could error out despite successful completion due to concurrent mark_completed() calls from the execution loop and worker wrapper.
  • Idempotent Transition: Made the Completed -> Completed state transition idempotent, allowing multiple calls to mark_completed() without error, treating subsequent calls as harmless no-ops.
  • Strictness Maintained: Ensured that other self-transitions (e.g., Pending -> Pending) remain rejected to preserve state machine strictness, narrowing the scope of the idempotency to only Completed -> Completed.
  • Improved Testing: Added new regression tests (test_completed_to_completed_is_idempotent, test_other_self_transitions_still_rejected) and updated an existing test (test_mark_completed_twice_is_idempotent) to thoroughly validate the fix and its narrow scope.
  • Debug Logging: Incorporated tracing::debug! for the idempotent early-return path to provide better visibility and debugging information when a redundant Completed -> Completed transition occurs.
Changelog
  • src/context/state.rs
    • Allowed Completed to Completed state transitions to be idempotent in the can_transition_to method.
    • Added an early return for idempotent state transitions in JobContext::transition_to with a debug log, preventing duplicate history entries.
    • Introduced test_completed_to_completed_is_idempotent to verify the idempotent behavior of the Completed state.
    • Added test_other_self_transitions_still_rejected to confirm that other self-transitions (e.g., Pending to Pending) are still rejected, ensuring the narrow scope of the fix.
  • src/worker/job.rs
    • Renamed test_mark_completed_twice_returns_error to test_mark_completed_twice_is_idempotent to reflect the updated behavior.
    • Updated the test_mark_completed_twice_is_idempotent test to assert that mark_completed is idempotent and returns Ok when called multiple times for a completed job, rather than an error.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@zmanian
zmanian enabled auto-merge (squash) March 12, 2026 19:47

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request correctly addresses a race condition by making the Completed -> Completed state transition idempotent. The changes are well-tested and effectively solve the described issue. Overall, this is a solid fix.

… race errors

Both execution_loop and the worker wrapper in execute() can race to call
mark_completed(). Previously the second call hit "Cannot transition from
completed to completed" and errored the job despite successful completion.

This narrowly allows only the Completed->Completed self-transition as
idempotent (early return with debug log, no duplicate history entry).
All other self-transitions remain rejected to preserve state machine
strictness.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@zmanian
zmanian force-pushed the fix/731-idempotent-job-completion branch from e1357a9 to 03c4445 Compare March 12, 2026 20:25
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@zmanian
zmanian merged commit 596d17f into staging Mar 16, 2026
10 checks passed
@zmanian
zmanian deleted the fix/731-idempotent-job-completion branch March 16, 2026 07:53
@ironclaw-ci ironclaw-ci Bot mentioned this pull request Mar 17, 2026
bkutasi pushed a commit to bkutasi/ironclaw that referenced this pull request Mar 28, 2026
… race errors (nearai#1068)

* fix(jobs): make completed->completed transition idempotent to prevent race errors

Both execution_loop and the worker wrapper in execute() can race to call
mark_completed(). Previously the second call hit "Cannot transition from
completed to completed" and errored the job despite successful completion.

This narrowly allows only the Completed->Completed self-transition as
idempotent (early return with debug log, no duplicate history entry).
All other self-transitions remain rejected to preserve state machine
strictness.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* style: fix assert! formatting in idempotent completion test

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
drchirag1991 pushed a commit to drchirag1991/ironclaw that referenced this pull request Apr 8, 2026
… race errors (nearai#1068)

* fix(jobs): make completed->completed transition idempotent to prevent race errors

Both execution_loop and the worker wrapper in execute() can race to call
mark_completed(). Previously the second call hit "Cannot transition from
completed to completed" and errored the job despite successful completion.

This narrowly allows only the Completed->Completed self-transition as
idempotent (early return with debug log, no duplicate history entry).
All other self-transitions remain rejected to preserve state machine
strictness.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* style: fix assert! formatting in idempotent completion test

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor: core 20+ merged PRs risk: medium Business logic, config, or moderate-risk modules scope: worker Container worker size: M 50-199 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants