fix(scheduling): make task registration mutually exclusive with shutdown (issue #216) #369
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
yuluo-yx
Describe what this PR does / why we need it
Issue #216 reports that
DefaultScheduledAgentManager.registerTaskcan 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 aftershutdown()has fully completed — the task lands inactiveTasksof 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
registerTaskchecked the volatile shutdown flag before acquiring the write lock.shutdown()stops every active task, clearsactiveTasks, 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 throwsIllegalStateExceptioninstead of inserting a stranded record. The lock type is deliberately unchanged —shutdown()callstask.stop()while holding the write lock andstop()re-enters the manager's write lock throughunregisterTask, which only works becauseReentrantReadWriteLockis reentrant.The rejection contract is now stated on the
ScheduledAgentManagerinterface: registration after shutdown is rejected, including a registration racing with an in-progress shutdown. A package-private constructor onDefaultScheduledAgentManagerlets tests inject a scheduler, since the eager singleton is not resurrectable and its real pool cannot drive deterministic interleaving.Testing
The new
ScheduledAgentManagerShutdownMutexTestreproduces the race deterministically, not by stress loops: shutdown holds the write lock inside a blockedSTOPPEDlistener 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-gatedgetName()hook, whichregisterTaskcalls 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