Repository navigation
fix(recap-subworker): exit on a frozen event loop and serialize native clustering - #257
Conversation
Covers what the subworker must do so a native-code freeze can no longer leave it alive but unresponsive: a GIL-independent loop watchdog that is armed at startup and re-armed from the loop, an orphan-run sweep that runs before the app serves requests, a Numba thread cap, a lock that keeps native clustering calls from overlapping (including after an HDBSCAN timeout leaves its thread running), blocking work kept off the event loop, and a ClassificationRunner shutdown that does not deadlock. App-booting tests stub the startup sweep so they need no database.
…e clustering Two in-process clustering runs entered native HDBSCAN at once and froze the whole process for days: the event loop never ran again, both listeners stopped accepting, and Docker kept the unhealthy container running because nothing exited. - A faulthandler timer, re-armed from the event loop, dumps every thread's stack and exits when the loop stalls for RECAP_SUBWORKER_LOOP_WATCHDOG_TIMEOUT_SECONDS (360, validated to exceed the HDBSCAN timeout); its timer thread does not need the GIL, so it fires even when native code holds it, and compose restarts the service. - Native clustering runs under one lock, and a timed-out HDBSCAN thread that is still running sends later calls to the MiniBatchKMeans fallback. - Orphaned 'running' rows are swept at startup through the DAO, before the app is exposed, instead of only on the next submission. - Pool start-up, result waits and lock waits in ClassificationRunner and the evidence/warmup handlers run off the event loop; acquiring the pool is one lock hold so cancellation cannot leak the task counter, and shutdown no longer re-acquires its own lock. - NUMBA_NUM_THREADS=1 joins the existing thread caps.
…ring scrapes Covers a healthy target, a 4-minute outage that stays pending, a 6-minute outage that fires at warning severity, and a target missing from the scrape config.
A frozen event loop times out the scrape as surely as a crash, so up == 0 (or the target being absent) for five minutes raises RecapSubworkerDown. It routes as a warning: the subworker only affects nightly recap and topic-cards jobs, so it should not page through quiet hours.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8266424ea9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| n_samples=reduced.shape[0], | ||
| min_cluster_size=effective_mcs, | ||
| ) | ||
| labels, probs = self._fallback_minibatch_kmeans(reduced) |
There was a problem hiding this comment.
Isolate timed-out HDBSCAN before starting the fallback
When HDBSCAN times out but its future remains active, this immediately starts MiniBatchKMeans while the abandoned native HDBSCAN call is still running. The future executes on its own executor thread and _run_with_timeout() uses shutdown(wait=False), so _NATIVE_CLUSTERING_LOCK—held by the waiting caller rather than the future—does not serialize these two native routines. This preserves the native-code overlap that caused the production freeze; the timed-out work needs process isolation or completion/termination before another native clustering implementation starts.
Useful? React with 👍 / 👎.
| - NUMBA_THREADING_LAYER=tbb | ||
| - NUMBA_NUM_THREADS=1 | ||
| # WHY: Must exceed RECAP_SUBWORKER_HDBSCAN_TIMEOUT_SECS (300s) so the watchdog does not preempt the HDBSCAN timeout fallback | ||
| - RECAP_SUBWORKER_LOOP_WATCHDOG_TIMEOUT_SECONDS=360 |
There was a problem hiding this comment.
Keep the watchdog configurable with the HDBSCAN override
When an operator sets the existing RECAP_SUBWORKER_HDBSCAN_TIMEOUT_SECS override to 360 seconds or more, Compose still pins the watchdog to 360, and the new validator rejects watchdog <= hdbscan; the container therefore enters a startup failure/restart loop. The HDBSCAN setting has no upper bound and is documented as configurable, so the Compose-first deployment should expose a corresponding watchdog substitution (or constrain/derive the two values together) instead of hard-coding it.
AGENTS.md reference: AGENTS.md:L41-L45
Useful? React with 👍 / 👎.
Summary
recap-subworker froze for 5.5 days: two in-process clustering runs entered native HDBSCAN at the same time, the asyncio loop never ran again, both listeners (:8002, :9443) filled their accept backlogs, and Docker kept the unhealthy container running because nothing exited. Every daily topic-cards run failed with a connect timeout in that window.
faulthandler.dump_traceback_later(exit=True), armed at startup and re-armed from the event loop. Its timer thread does not need the GIL, so a native freeze still dumps every thread's stack and exits; composerestart: unless-stoppedbrings the service back.RECAP_SUBWORKER_LOOP_WATCHDOG_TIMEOUT_SECONDS=360, validated to exceed the HDBSCAN timeout so it never preempts the existing fallback.runningrows are failed through the DAO before the app is exposed, not only on the next submission.asyncio.to_thread. Acquiring the pool is a single lock hold so cancellation cannot leak the task counter, andshutdown()no longer deadlocks on its own lock.NUMBA_NUM_THREADS=1joins the existing thread caps.RecapSubworkerDown(up == 0or absent for 5m, severitywarning).Test plan
uv run pytest tests/unit -q: 624 passed, 13 skippeduv run pytest tests/integration/test_learning_flow.py -qtests/unit/test_tooling.py)promtool check rulesandpromtool test ruleson all rule testsloop_watchdog_enabledlogged, runs 14964 / 14965 swept tofailed🤖 Generated with Claude Code