[Optimus] fix: eliminate TOCTOU race in Meta-Cron concurrency guard (fixes #511)#513
Merged
[Optimus] fix: eliminate TOCTOU race in Meta-Cron concurrency guard (fixes #511)#513
Conversation
The hourly patrol overlap bug occurred because: 1. tick() used isLocked() (read-only check) then fire() called createLock() separately — a TOCTOU gap allowing two ticks to both see unlocked state 2. Lock files recorded the MCP server PID, not the child worker PID, causing isPidRunning() to check the wrong process after server restarts 3. The 1-hour time-based staleness threshold exactly matched the hourly cron period, creating an edge case at the tick boundary Changes: - Move atomic createLock() into tick() as the single concurrency guard - Add updateLockPid() to write child process PID after spawn - Increase time-based staleness threshold from 1h to 2h (matches safety timer) - Release lock on tick-dedup rejection and dry-run paths - Add 9 vitest unit tests for lock lifecycle Co-Authored-By: Claude <noreply@anthropic.com>
cloga
pushed a commit
that referenced
this pull request
Mar 20, 2026
…follow-up) The overlap recurrence at 15:00Z (Runs #215/#216) was caused by the old MCP server process still running pre-fix code after PR #513 merged. This is not a code bug — the fix requires a server restart to take effect. However, this commit also hardens the leader election against a latent TOCTOU race where two processes simultaneously detect a stale leader, both unlink+create, and one deletes the other's freshly created lock: - Add unique nonce to scheduler leader lock data - After stale-leader reclaim (unlink+wx create), re-read the lock and verify the nonce matches before confirming leadership - If nonce mismatches, another process won the race — back off Also cleaned up the dead scheduler-leader.lock (PID 362356). Co-Authored-By: Claude <noreply@anthropic.com>
cloga
added a commit
that referenced
this pull request
Mar 20, 2026
…follow-up) The overlap recurrence at 15:00Z (Runs #215/#216) was caused by the old MCP server process still running pre-fix code after PR #513 merged. This is not a code bug — the fix requires a server restart to take effect. However, this commit also hardens the leader election against a latent TOCTOU race where two processes simultaneously detect a stale leader, both unlink+create, and one deletes the other's freshly created lock: - Add unique nonce to scheduler leader lock data - After stale-leader reclaim (unlink+wx create), re-read the lock and verify the nonce matches before confirming leadership - If nonce mismatches, another process won the race — back off Also cleaned up the dead scheduler-leader.lock (PID 362356). Co-authored-by: Long Chen (from Dev Box) <lochen@microsoft.com> Co-authored-by: Claude <noreply@anthropic.com>
3 tasks
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.
Summary
Diagnoses and fixes the recurring hourly patrol overlap bug (Issue #511) where
concurrency_policy: "Forbid"was violated, allowing Run #213 and Run #214 to execute concurrently.Root Cause (3 compounding issues)
TOCTOU race:
tick()usedisLocked()(read-only) for the concurrency check, thenfire()calledcreateLock()separately. Two ticks within the same minute could both passisLocked() → falsebefore either created the lock.Wrong PID in lock: Lock files recorded
process.pid(the MCP server), not the spawned child worker PID. When the MCP server restarted,isPidRunning()saw the old server PID as dead and treated the lock as stale — even though the child worker was still running.Staleness threshold == cron period: The 1-hour time-based staleness threshold exactly matched the hourly cron period (
0 * * * *), creating an edge case where the lock was declared stale at the exact moment the next tick fired.Changes
isLocked()check + separatecreateLock()pattern with a singlecreateLock()call intick()as the atomic concurrency gate (usesO_CREAT|O_EXCL/'wx'flag).spawn(), soisPidRunning()checks the actual worker.deleteLock()calls for tick-dedup rejection and dry-run code paths.Test Results
npm run build— exit 0, zero errorssrc/mcp/meta-cron-engine.ts,src/test/meta-cron-locks.test.ts,optimus-plugin/dist/mcp-server.js(rebuilt)Closes #511
🤖 Created by
senior-full-stack-buildervia Optimus Spartan Swarm