Skip to content

[ML] Fix lost shutdown wakeup in the spawned process tracker thread - #3287

Merged
edsavage merged 2 commits into
elastic:mainfrom
edsavage:ml/fix-tracker-thread-lost-wakeup
Nov 23, 2026
Merged

edsavage merged 2 commits into
elastic:mainfrom
edsavage:ml/fix-tracker-thread-lost-wakeup

Conversation

@edsavage

@edsavage edsavage commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR fixes a lost wakeup in CTrackerThread::run() (POSIX CDetachedProcessSpawner) that makes ~CDetachedProcessSpawner block forever in pthread_join.

run() read m_Shutdown outside the mutex (while (!m_Shutdown)) and only then took the mutex and called m_Condition.wait(). If shutdown() 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 of m_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 on main and 9.6; not on 9.5 or 8.19. The Windows tracker still holds the lock across the loop and is unaffected.

Evidence

  • A hang caught by the watchdog in [9.6] [ML] Report hung test batches to Test Analytics and fail them fast #3285 (build 3441, 9.6, macOS): main thread in ~CDetachedProcessSpawner -> CThread::stop -> pthread_join; tracker thread in CCondition::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).
  • CTest timeouts in snapshot builds on main/9.6 since 28 Oct: 7310, 7316, 7361. None between 1 Aug and 25 Oct.
  • A/B testing on the real code, with only the run() change differing, running the new test testCreateAndDestroyImmediately (5000 create/destroy cycles) repeatedly on macOS arm64: buggy run() hung in 29 of 29 runs; fixed run() passed 300 of 300 (and 50 of 50 after the final edit). All 7 CDetachedProcessSpawnerTest cases pass.
  • A standalone reproduction of the loop with the same pthread primitives hung in 3 of 5 runs of 200k cycles (buggy) against 0 of 5 (fixed).

Notes

edsavage and others added 2 commits November 5, 2026 10:13
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>
@edsavage
edsavage requested a balanced review from Copilot November 17, 2026 02:34
@edsavage
edsavage marked this pull request as ready for review November 17, 2026 02:34

Copilot AI 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.

🟢 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

@elasticsearchmachine

Copy link
Copy Markdown

Pinging @elastic/ml-core (Team:ML)

@edsavage edsavage added v9.6.1 auto-backport Automatically merge backport PRs when CI passes labels Nov 19, 2026
@edsavage

Copy link
Copy Markdown
Contributor Author

Fresh evidence of this hang on main: the daily snapshot build 7483 (22 Nov) failed in Test :cpp: for linux-x86_64-RelWithDebInfo.

  • 853 of 854 tests passed. The failure was CDetachedProcessSpawnerTest_testKill:CDetachedProcessSpawnerTest_testPermitted (test Add a CHANGELOG file for release notes #78), which hit the 2700 s CTest timeout.
  • It started with the first batch of tests. Every other test had finished within about 230 s, then the job waited for Add a CHANGELOG file for release notes #78 until the timeout.
  • All JUnit summaries report 0 failures, so no assertion failed: the batch never returned.

This is consistent with the lost wakeup described above, since main has #3217 and not this fix. It is circumstantial: the log has no stack dump or thread state to confirm the tracker thread, and 9.6 (which also has #3217) passed its last two daily builds, so the hang is intermittent.

@valeriy42 valeriy42 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.

lgtm. Thank you for fixing this.

@edsavage
edsavage merged commit 7fe8350 into elastic:main Nov 23, 2026
38 checks passed
@elastic-vault-github-plugin-prod

Copy link
Copy Markdown
Contributor

💚 All backports created successfully

Status Branch Result
✅ 9.6

Questions ?

Please refer to the Backport tool documentation and see the Github Action logs for details

elastic-vault-github-plugin-prod Bot added a commit that referenced this pull request Nov 23, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-backport Automatically merge backport PRs when CI passes >bug :ml v9.6.1 v9.7.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants