Skip to content

fix(realtime): report a Rust run that holds the database without progress as stalled - #525

Open
fstrasz wants to merge 6 commits into
chunkhound:mainfrom
fstrasz:fix/rust-hold-stall-main
Open

fstrasz wants to merge 6 commits into
chunkhound:mainfrom
fstrasz:fix/rust-hold-stall-main

Conversation

@fstrasz

@fstrasz fstrasz commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem

While a Rust indexing run holds the database, nothing reports that the run has hung.

run_rust_indexing_phase sets the Rust-pipeline flag, releases the database and awaits the native pipeline.run in a thread. There is no ceiling and no cancellation, and the flag clears only when the call returns. The initial scan runs with no resync pending.

So if that Rust run hangs (a deadlocked parse worker, for example):

  • the live state reads idle (empty backlog) or busy;
  • daemon_status stays initializing;
  • the hang is reported nowhere.

Without a progress display, no progress callback was passed at all.

Fix

Two commits.

1. Report a hold with no progress as stalled.

  • Heartbeat: SerialDatabaseExecutor keeps a heartbeat for the hold: the monotonic time and phase of the latest progress callback. It is stamped when the flag is set and cleared with it.
    • note_rust_pipeline_progress writes it directly, never through execute/submit, which fail fast while the flag is set.
    • rust_pipeline_progress() returns (idle seconds, last phase) during a hold.
    • SerialDatabaseProvider and the DatabaseProvider protocol delegate both.
  • Callback: run_rust_indexing_phase always passes a progress callback. It notes progress on the database, then forwards to the progress bridge when there is one.
  • Status read:
    • The live-state derivation reads the stalled hold once, after its degraded and uninitialized checks, and returns it with the state for the hint.
    • A hold idle past its ceiling reads stalled even with an empty backlog. The aggregate daemon status then reports degraded.
    • The hint names the idle time and the last phase, or says that no progress has arrived since the run started.
  • Constant: _COMPACTION_OPERATION_TIMEOUT_SECONDS becomes public (COMPACTION_OPERATION_TIMEOUT_SECONDS), because the realtime service now reads it. Its test users are updated.

2. Pick the ceiling from the store stage's step. The phases come from different Rust threads (src/pipeline/pipeline.rs):

  • diff, parse, embed and done come from the main, parse and embed threads;
  • every write-* phase comes only from the store thread.

So "the last phase from any thread" doesn't say what the store is doing:

  • While the store thread opens the database, it rebuilds any missing HNSW index with no callback. A long rebuild read stalled at 10 minutes.
  • After a disk-limit trip, a late parse or embed report could overwrite write-index and drop the ceiling to 10 minutes while that single native step ran.

The executor now keeps one heartbeat per stage: the main stage (main, parse and embed threads) and the store stage (the store thread). Each stage's report is a single attribute store of a (time, phase) tuple, so it never tears.

  • Read order: readers load the attributes before checking the hold flag, and the hold's end clears the flag before the attributes. A hold ending mid-read therefore can't make the whole hold read idle.
  • Ceiling:
    • 22 minutes (2 × the compaction timeout) while the store has not reported yet, or is in write-compact / write-index, which are single native steps with no callback until write-done;
    • 10 minutes for any other store phase.
  • Return shape: rust_pipeline_progress() keeps its shape.
  • New accessor: rust_pipeline_store_phase() returns the store stage's latest phase.
  • Reads in the service: the service reads the store phase first and then the progress. Every interleaving with a concurrent report, hold start or hold end is then safe.

Trade-off: a hang before the store thread's first report reads stalled after 22 minutes instead of 10. That covers the diff phase and the store's open, HNSW rebuild, HNSW drop and orphan deletes.

Not fixed, needs a native change:

  • Stopping a hung native run: Python cannot interrupt it. It is now reported so it can be restarted.
  • Store error after the HNSW drop: the error path's backend.close() rebuilds the HNSW indexes with no callback, under the last write-data/write-prepare phase. A long rebuild there can still read stalled at 10 minutes before the run fails.

Rejected:

  • Releasing parked writes to the budgeted retry path. It turns a visible stall into per-file retry failures, and those cannot succeed while the database is held.
  • Emitting write-index from Rust before the open-time rebuild. That needs a native release.

Tests

tests/realtime/test_realtime_status_read.py uses the public progress methods and a daemon_status read with time.monotonic advanced:

Test Expected Before this PR
test_hung_rust_hold_reads_stalled (real DuckDB provider and executor) An empty backlog under a hold idle 601 s in write-data reads stalled, with the hold hint and aggregate degraded; 590 s, and write-compact at 700 s, do not Read idle
test_late_main_report_keeps_the_store_step_ceiling write-index, then a late parse; at +700 s reads idle Read stalled
test_hold_before_the_store_reports_uses_the_long_ceiling parse and embed with no store report; at +700 s reads idle, at +1321 s stalled Read stalled at +700 s

tests/unit/test_rust_pipeline_runner.py: test_rust_run_progress_is_noted_without_progress_display checks that, with no progress display, a write-data callback is noted. It fails with no callback, and also with a callback that notes nothing.

Each of four mutations of the stage logic made a test fail.

These were run serially on a merge of this branch with current main (24752d0): tests/realtime, the realtime functional and failure tests, tests/contracts, and the compaction, execute-timeout, runner and compaction-retry unit tests. Results: 429 passed / 38 skipped on main, 433 / 38 merged.

ruff and mypy report the same findings as main on the changed files. Upstream's CI workflow on our fork at this head (3a14141) was green on every job on 2026-11-11, under Rust 1.99.1: https://github.com/fstrasz/chunkhound/actions/runs/43083267969

This PR changes no Rust code.

Not reproduced end to end: a real hung native run can't be induced from Python. The tests drive the heartbeat through the public methods instead.

Overlap

This PR is standalone on main and depends on no open PR.

These merge cleanly with main today and conflict mechanically with this PR. Whichever merges second rebases:

These already conflict with main, and also edit files this PR changes:

CHANGELOG.md: [Unreleased] → Fixed.

🤖 Generated with Claude Code, reviewed by FStrasz

fstrasz and others added 2 commits November 11, 2026 09:54
…ress as stalled

Root cause: run_rust_indexing_phase sets the Rust-pipeline flag, releases
the database and awaits the native pipeline.run in a thread, with no
ceiling and no cancellation; the flag clears only when the call returns.
The initial scan runs with no resync pending, so if its Rust run hangs
(a deadlocked parse worker, for example) the live state reads idle
(empty backlog) or busy, daemon_status stays initializing, and the hang
is reported nowhere. No progress callback was passed at all without a
progress display.

Design:
- SerialDatabaseExecutor keeps a heartbeat for the hold: the monotonic
  time and phase of the last progress callback, stamped when the flag is
  set and cleared with it. note_rust_pipeline_progress writes the two
  attributes directly, never through execute/submit (they fast-fail
  while the flag is set). rust_pipeline_progress returns (idle seconds,
  last phase) during a hold. SerialDatabaseProvider and the
  DatabaseProvider protocol delegate both. The callback writes off the
  event loop and the status read happens on it; the two stores are
  unlocked because a torn read can only pair a new time with the
  previous phase, which changes the ceiling of that one read.
- run_rust_indexing_phase always passes a callback taking write-data's
  four arguments; it notes progress on the database, then forwards to
  the progress bridge when one exists.
- The status read computes the stalled hold once and passes it to both
  the state and the hint, so they cannot disagree. The state checks it
  after degraded and uninitialized and before busy/idle: a hold idle
  past its ceiling reads stalled even with an empty backlog, which the
  aggregate daemon status reports as degraded. The ceiling is 600 s, or
  2 x the compaction timeout (1320 s) when the last phase is
  write-compact or write-index, single native steps with no callback
  until write-done. The hint names the idle time and the last phase, or
  says no progress has arrived since the run started.
- _COMPACTION_OPERATION_TIMEOUT_SECONDS becomes the public
  COMPACTION_OPERATION_TIMEOUT_SECONDS, since the realtime service now
  reads it; its test users are updated.
- Rejected: releasing parked writes to the budgeted retry path. That
  turns a visible stall into per-file retry-budget failures while the
  database is still held, so they cannot succeed anyway.
- Not done: stopping a hung native run, which Python cannot interrupt.

Tests:
- test_hung_rust_hold_reads_stalled: real DuckDB provider and executor,
  read through daemon_status; an empty backlog under a hold idle 601 s
  reads stalled with the hold hint and aggregate degraded; 590 s, and
  write-compact at 700 s, do not. Reads idle without the fix.
- test_rust_run_progress_is_noted_without_progress_display: with no
  progress display, the phase is None when the hold starts and is
  write-data after a write-data callback, read through
  rust_pipeline_progress. Fails without the fix (no callback) and with a
  no-op note.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s step

Root cause

The serial executor kept one (time, phase) heartbeat for a Rust hold,
written by whichever Rust thread reported last, and the realtime service
picked the stall ceiling from that last phase (22 minutes for
write-compact/write-index, else 10). The phases come from different
threads in src/pipeline/pipeline.rs: diff, parse, embed and done from the
main, parse and embed threads; every write-* phase only from the store
thread. So the last phase does not say what the store thread is doing:

1. The store thread opens the database first (backend.open(),
   pipeline.rs ~650), which rebuilds any missing HNSW index with no
   callback (src/db/duckdb_backend/mod.rs:101). The last phase is then
   parse, embed or None, so a 10-22 minute rebuild read stalled at 10.
2. After a disk-limit trip the store thread drops store_rx
   (pipeline.rs:833-835) and emits write-index (pipeline.rs:893) before
   building the HNSW indexes, while the parse and embed threads can still
   report their in-flight batch (the parse thread emits at pipeline.rs:619
   before its send at :621; embed likewise before store_tx.send). That late
   report overwrote the phase and dropped the ceiling to 10 minutes while
   the single native step ran. The main thread's own final reports
   (pipeline.rs:1139-1142) come after it joins all three stages
   (:1084-1086), so they cannot land during compact or index.

Design

The executor keeps one heartbeat per stage: the main stage (main, parse
and embed threads) and the store stage (the store thread, the only one
that reports write-* phases), plus the hold's start time. Each stage's
report is one attribute store of a (time, phase) tuple, written by that
stage's threads and read on the event loop, so its time and phase never
tear; no lock is needed, and a read can at worst combine one stage's
newer report with the other's previous one. The readers load the
attributes before checking the hold flag, and the hold's end clears the
flag before the attributes, so a read that still sees the flag set got
this hold's values; otherwise a hold ending mid-read (the status can be
built on the watchdog observer thread) could pair a live start time
with cleared reports and report the whole hold as idle.

rust_pipeline_progress() keeps its shape, (seconds since the last report
from any stage, its phase) or None outside a hold, so its callers and the
status hint are unchanged. A new rust_pipeline_store_phase() returns the
store stage's latest phase, or None outside a hold or before its first
report.

The service reads the store phase first, then the progress. A report
landing between the two reads then only refreshes the idle time, a hold
ending between them reads None, and a hold starting between them reads
the long ceiling, so every interleaving is safe. In the other order a
store report between the reads could pair an old long idle time with a
new short ceiling and read a false stall.

Ceiling rule: no store report yet, or the store stage in write-compact or
write-index, gets 22 minutes; any other store phase gets 10. Stalled when
the idle time exceeds the ceiling.

Trade-off

A hang before the store thread's first report (the diff phase, or the
store's open, HNSW rebuild, HNSW drop and orphan deletes; write-prepare is
emitted right after those and before any batch) reads stalled after 22
minutes instead of 10. This is not gated on HNSW being enabled: there is
no switch for it, and the native open path rebuilds whatever HNSW indexes
the schema expects.

Residual not fixed

On a store error after the HNSW drop, the error path's backend.close()
rebuilds every HNSW index (hnsw_bulk_mode, mod.rs:107-111) with no
callback under the last write-data or write-prepare phase, so a long
rebuild can still read stalled at 10 minutes before the run fails. That
needs a native emit.

Rejected

- Emitting write-index from Rust before the open-time rebuild: needs a
  native release.
- One heartbeat that ignores main-stage reports once the store has
  reported: loses the main threads' heartbeat.

Tests

tests/realtime/test_realtime_status_read.py, through the public progress
methods and a daemon_status read with time.monotonic advanced:
- test_hung_rust_hold_reads_stalled, rewritten off the private
  attributes: write-data at +601 s reads stalled (aggregate degraded), at
  +590 s idle; write-compact at +700 s idle.
- test_late_main_report_keeps_the_store_step_ceiling: write-index then
  parse, +700 s reads idle. Read stalled before this change.
- test_hold_before_the_store_reports_uses_the_long_ceiling: parse and
  embed with no store report, +700 s reads idle and +1321 s stalled. Read
  stalled at +700 s before this change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@HananKavitzAmat

Copy link
Copy Markdown
Collaborator

@fstrasz can you resolve the merge issue?

Main is at e776eba (v6.2.0). The only conflict was CHANGELOG.md: 6.2.0 is
released, so this PR's "A hung Rust indexing run is reported" entry moves to
[Unreleased] -> Fixed, and the 6.2.0 section is upstream's unchanged. The
other files merged cleanly, and the PR's diff against main in its nine code
and test files is line-for-line the same as before the merge.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fstrasz

fstrasz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Resolved: I merged current main (e776eba, v6.2.0) into the branch. The only conflict was in CHANGELOG.md: 6.2.0 is now released, so this PR's entry moved to [Unreleased] → Fixed. The other files merged cleanly, and the PR's diff against main is unchanged in all nine code and test files.

🤖 Generated with Claude Code, reviewed by FStrasz

chunkhound#509's overflow test builds the realtime service with no database provider
and reads its health. The status read computed the stalled Rust hold before
deriving the live state, so every read touched the provider, and that test
failed with "'NoneType' object has no attribute 'rust_pipeline_store_phase'".

The state derivation now reads the hold itself, after the degraded and
uninitialized checks, and returns it with the state, so the state and its
hint still come from one read. A degraded or uninitialized service no longer
touches the provider. The hint uses the hold only for a stalled state, so no
reported state or hint changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fstrasz

fstrasz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

The shard 1 failure came from #509's overflow test, which builds the realtime service with no database provider; this PR's status read touched the provider on every read. be854ab reads the Rust hold only after the degraded and uninitialized checks, so that test passes and no reported state changes.

🤖 Generated with Claude Code, reviewed by FStrasz

Resolves the CHANGELOG.md conflict under [Unreleased]: upstream's Added,
Changed, Fixed and Removed sections are kept as they are, and this PR's
Fixed entry ("A hung Rust indexing run is reported") is added at the end
of upstream's Fixed list, so there is one Fixed heading, in Keep a
Changelog order. No other file conflicted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
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.

2 participants