Repository navigation
[ML] Fix lost shutdown wakeup in the spawned process tracker thread - #3287
Conversation
CTrackerThread::run() checked m_Shutdown outside the mutex and only then took the mutex and waited. If shutdown() set the flag and signalled in between, the signal was lost and, with no children to reap, the thread waited forever, so ~CDetachedProcessSpawner blocked in pthread_join. This was introduced by the restructuring in elastic#3217 (the old loop held the mutex across the check and the wait). It has been hanging unit tests in CI since mid October. Check the flag under the mutex immediately before waiting. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block approval of the detached-process tracker shutdown change.
No open issues 🎉
What changed in this PR
Fixes a shutdown race in the POSIX detached-process tracker that could leave the controller blocked indefinitely while destroying a process spawner.
Changes:
- Moves the shutdown check under the condition-variable mutex, preventing lost wakeups and unsynchronized flag reads.
- Adds a regression test covering 5,000 immediate spawner creation/destruction cycles.
- Records the fix in the changelog.
| File | Description |
|---|---|
lib/core/unittest/CDetachedProcessSpawnerTest.cc |
Adds repeated immediate-shutdown coverage with no child processes. |
lib/core/CDetachedProcessSpawner.cc |
Checks shutdown while holding the mutex before waiting. |
docs/changelog/3287.yaml |
Documents the spawned-process tracker shutdown fix. |
Review effort: Balanced
|
Pinging @elastic/ml-core (Team:ML) |
|
Fresh evidence of this hang on
This is consistent with the lost wakeup described above, since |
valeriy42
left a comment
There was a problem hiding this comment.
lgtm. Thank you for fixing this.
💚 All backports created successfully
Questions ?Please refer to the Backport tool documentation and see the Github Action logs for details |
…3287) (#3326) CTrackerThread::run() checked m_Shutdown outside the mutex and only then took the mutex and waited. If shutdown() set the flag and signalled in between, the signal was lost and, with no children to reap, the thread waited forever, so ~CDetachedProcessSpawner blocked in pthread_join. This was introduced by the restructuring in #3217 (the old loop held the mutex across the check and the wait). It has been hanging unit tests in CI since mid October. Check the flag under the mutex immediately before waiting. (cherry picked from commit 7fe8350) Co-authored-by: Ed Savage <ed.savage@elastic.co>
Summary
This PR fixes a lost wakeup in
CTrackerThread::run()(POSIXCDetachedProcessSpawner) that makes~CDetachedProcessSpawnerblock forever inpthread_join.run()readm_Shutdownoutside the mutex (while (!m_Shutdown)) and only then took the mutex and calledm_Condition.wait(). Ifshutdown()sets the flag and signals between those two steps, the signal is lost, and with no children the thread uses the untimed wait and never wakes. The check is now made under the mutex immediately before waiting.shutdown()sets the flag and signals under the same mutex, so the wakeup can no longer be missed. This also removes the unsynchronised read ofm_Shutdown.I think this was introduced by the restructuring in #3217 (
0df773aae, 16 Oct); the previous loop held the mutex across the check and the wait. Present onmainand9.6; not on9.5or8.19. The Windows tracker still holds the lock across the loop and is unaffected.Evidence
~CDetachedProcessSpawner->CThread::stop->pthread_join; tracker thread inCCondition::wait()->pthread_cond_wait; machine idle (load 0.5). This is the same location as the router-test hang in snapshot build 7361 (inferred from its last log line).run()change differing, running the new testtestCreateAndDestroyImmediately(5000 create/destroy cycles) repeatedly on macOS arm64: buggyrun()hung in 29 of 29 runs; fixedrun()passed 300 of 300 (and 50 of 50 after the final edit). All 7CDetachedProcessSpawnerTestcases pass.Notes
CLoggerTest_testWarnAndErrorThrottling(named pipe), and 9.5 does not have [ML] Reap child IPC dirs and fail closed on degraded seccomp #3217, so I believe that's a separate problem, tracked in [ML] 9.5: CLoggerTest_testWarnAndErrorThrottling batch hangs to the CTest timeout (macOS aarch64, build 7294) #3320 and not addressed here.