Skip to content

fix(recap-subworker): exit on a frozen event loop and serialize native clustering - #257

Merged
Kaikei-e merged 4 commits into
mainfrom
fix/recap-subworker-liveness
Oct 1, 2026
Merged

Kaikei-e merged 4 commits into
mainfrom
fix/recap-subworker-liveness

Conversation

@Kaikei-e

@Kaikei-e Kaikei-e commented Oct 1, 2026

Copy link
Copy Markdown
Owner

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.

  • Loop watchdog: 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; compose restart: unless-stopped brings the service back. RECAP_SUBWORKER_LOOP_WATCHDOG_TIMEOUT_SECONDS=360, validated to exceed the HDBSCAN timeout so it never preempts the existing fallback.
  • Native clustering lock: UMAP / FAISS / HDBSCAN / DBCV / silhouette run one call at a time; while a timed-out HDBSCAN thread is still running, later calls go to the MiniBatchKMeans fallback.
  • Startup orphan sweep: running rows are failed through the DAO before the app is exposed, not only on the next submission.
  • Off-loop work: ClassificationRunner pool start-up, result waits and lock waits, plus the evidence/warmup handlers, run via asyncio.to_thread. Acquiring the pool is a single lock hold so cancellation cannot leak the task counter, and shutdown() no longer deadlocks on its own lock.
  • NUMBA_NUM_THREADS=1 joins the existing thread caps.
  • Alert: RecapSubworkerDown (up == 0 or absent for 5m, severity warning).

Test plan

  • uv run pytest tests/unit -q: 624 passed, 13 skipped
  • uv run pytest tests/integration/test_learning_flow.py -q
  • ruff check / format, pyrefly (tests/unit/test_tooling.py)
  • promtool check rules and promtool test rules on all rule tests
  • After deploy: subworker healthy, loop_watchdog_enabled logged, runs 14964 / 14965 swept to failed

🤖 Generated with Claude Code

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-01T09:45:22.596356Z 8266424 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment thread compose/recap.yaml
- 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@Kaikei-e
Kaikei-e merged commit b829812 into main Oct 1, 2026
68 checks passed
@Kaikei-e
Kaikei-e deleted the fix/recap-subworker-liveness branch October 1, 2026 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant