← Back to Pull requests

fix(scheduling): make task registration mutually exclusive with shutdown (issue #216) #369

Closed
unknown closed this pull request without merging on Nov 15, 2026 14:57 UTC
View on GitHub
unknown unknown commented on Nov 14, 2026 03:52 UTC
Author

Describe what this PR does / why we need it

Issue #216 reports that DefaultScheduledAgentManager.registerTask can strand a task inside a shut-down manager: the shutdown check sits outside the write lock, so a registration that passes it can be interleaved after shutdown() has fully completed — the task lands in activeTasks of a manager whose scheduler is closed, is never stopped, and leaves the active count non-zero forever. This PR makes check-and-register atomic with shutdown, documents the rejection contract on the interface, and adds deterministic interleaving regression tests.

Root cause

registerTask checked the volatile shutdown flag before acquiring the write lock. shutdown() stops every active task, clears activeTasks, closes the scheduler, and sets the flag — all under the same write lock. A registrant passing the flag check can be preempted before locking; when it finally acquires the lock, it inserts into a manager that is already shut down. The pre-lock check is not synchronized with the flag write, so the two operations race.

The fix

The same rejection is re-checked inside the write lock critical section (the pre-lock fast-fail check stays, so the uncontended path does not pay for the lock). Because shutdown() sets the flag under that very lock, the in-lock re-check closes the interleaving window: any registration that wins the write lock after shutdown has started sees the flag and throws IllegalStateException instead of inserting a stranded record. The lock type is deliberately unchanged — shutdown() calls task.stop() while holding the write lock and stop() re-enters the manager's write lock through unregisterTask, which only works because ReentrantReadWriteLock is reentrant.

The rejection contract is now stated on the ScheduledAgentManager interface: registration after shutdown is rejected, including a registration racing with an in-progress shutdown. A package-private constructor on DefaultScheduledAgentManager lets tests inject a scheduler, since the eager singleton is not resurrectable and its real pool cannot drive deterministic interleaving.

Testing

The new ScheduledAgentManagerShutdownMutexTest reproduces the race deterministically, not by stress loops: shutdown holds the write lock inside a blocked STOPPED listener callback while a registration thread passes the pre-lock check and blocks on the lock (the gap between check and lock is observed through a latch-gated getName() hook, which registerTask calls exactly there). Releasing the shutdown lets the manager finish; before the fix the registration then inserts a stranded record (red: count 1, no exception), after the fix the in-lock re-check rejects it (count 0, IllegalStateException). Two further cases pin the contract: a registration after a completed shutdown is rejected with no residue, and concurrent stop/shutdown threads remain idempotent with no leftover records. The scheduling package is 17/17 green (including the #214 ownership regressions), checkstyle and spring-javaformat are clean. The module has 8 pre-existing failures on this baseline (skills prompt template, file store Windows path, prompt template line endings) that this PR does not touch.

Fixes #216

Diff: 407 lines (+305 / -102) across 5 files Reviewer: yuluo-yx Review duration: 2104m
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.45) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 407 lines diff across 5 files
0.45
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
56%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.360
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #369"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 56.8% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 44.8% (MEDIUM) · Higher = larger blast radius
44.8%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 56.8% · Lower = rushed or shallow review
56.8%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 19.4% · Safe to merge
19.4%