Skip to content

[Iterator Revalidation][MOD-17393] Keep exhaustion terminal across revalidate and resume - #10839

Merged
LukeMathWalker merged 5 commits into
masterfrom
luca-eof-terminal-across-revalidate
Aug 13, 2026
Merged

LukeMathWalker merged 5 commits into
masterfrom
luca-eof-terminal-across-revalidate

Conversation

@LukeMathWalker

@LukeMathWalker LukeMathWalker commented Aug 10, 2026 •

Copy link
Copy Markdown
Collaborator

Describe the changes in the pull request

Current. An iterator that has run past its last result must stay there until rewind: composites drop children that report EOF and cannot take them back. Inverted-index leaves broke that. revalidate and resume_in_place restored position by rewinding and re-seeking, and the rewind clears the past-the-end state — so an exhausted leaf came back live on a document it had already yielded. Both unions carried compensation code naming this exact cause, and interop.rs left C and Rust disagreeing about whether a child was still alive. NotOptimized reached the same end by its own route: skip_to past max_doc_id ends it without touching its wildcard, and it then re-derived its EOF flag from that wildcard — so any later forward move by the wildcard cleared it.

Change. Exhaustion is terminal on both paths: leaves restore the past-the-end position instead of seeking back to it, and NotOptimized latches its EOF flag. Full unions now assert that no child sits behind them rather than absorbing one that does. New tests cover all five leaves on both paths — the existing at-EOF tests never dirtied the index, so revalidate returned before reaching the rewind, which is how this survived a harness built to catch it.

Outcome. A resurrected child can no longer replay documents into a parent that has moved on, enforced by assertions and tests rather than by parents compensating. Each fix was confirmed red before green.

Notes for reviewers

  • There is a behaviour change, narrower than a release note usually covers. When documents were indexed after a leaf exhausted and GC ran on that index, the old re-seek reported Moved and the leaf went on to yield those newer documents. It no longer does. I read that as bringing the GC path in line with the no-GC path, where such documents were never picked up. Flip the release-notes box if you disagree — the title would then need to state the user impact.
  • Unions assert in debug and still recover in release, so a contract violation is impossible to miss in CI without crashing a live query.
  • The NotOptimized latch fixes a reachable case, not a theoretical one, and it needs no misbehaving child to get there: a wildcard moving forward under GC is all a conforming one ever does. That is the second half of the release-notes call above — the symptom is an iterator yielding again after reporting it had nothing left. revalidate_after_skip_past_max_doc_id_does_not_revive_the_iterator covers it; with the old assignment restored, ContractChecker fails it with "an exhausted iterator must keep returning None, but it yielded doc 60".

Verification. cargo nextest run, make lint, make fmt CHECK=1 green on the current head. Miri (528) and ./build.sh RUN_PYTEST were green on the first commit — 61 pre-existing environmental failures, none touching query iteration — and no commit since adds unsafe.

Which additional issues this PR fixes

None.

Main objects this PR modified

  1. RawInvIndIterator::{revalidate, resume_in_place} — rqe_iterators/src/inverted_index/core.rs
  2. NotOptimized::revalidate — rqe_iterators/src/not_optimized.rs
  3. UnionFlat/UnionHeap revalidate — assertion in place of the compensation
  4. revalidate_via_resume, ContractChecker, and revalidate_at_eof_after_gc across the five leaves
  5. RQEIterator::at_eof, which now owns the exhaustion contract

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

Release Notes

  • This PR requires release notes
  • This PR does not require release notes

@codecov

codecov Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.52239% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.09%. Comparing base (d242117) to head (4d50090).
⚠️ Report is 29 commits behind head on master.

Files with missing lines Patch % Lines
.../redisearch_rs/rqe_iterators_test_utils/src/lib.rs 89.47% 1 Missing and 1 partial ⚠️
...search_rs/rqe_iterators/src/inverted_index/core.rs 96.96% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10839      +/-   ##
==========================================
- Coverage   82.95%   82.09%   -0.87%     
==========================================
  Files         606      630      +24     
  Lines       80704    82136    +1432     
  Branches    35621    37397    +1776     
==========================================
+ Hits        66951    67427     +476     
- Misses      13414    14361     +947     
- Partials      339      348       +9     
Flag Coverage Δ
flow 84.35% <ø> (+0.39%) ⬆️
unit 55.60% <95.52%> (-0.27%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

An iterator that has run past its last result must stay past it until `rewind`.
The trait says so, composites rely on it — a union drops children that report
`at_eof`, `Intersection::revalidate` early-returns on `is_eof` — and
`ContractChecker` asserts it. It was violated anyway.

`RawInvIndIterator::revalidate` and `resume_in_place` both restore position with
`rewind()` followed by `skip_to(last_doc_id)`. `rewind` clears `at_eos`, so for an
iterator that was *at* EOF the re-seek finds the document it already yielded and it
comes back reporting `at_eof() == false` and `Ok` — claiming its position never
changed while EOF silently flipped. A composite that had dropped the child on EOF
then re-admits it behind the position it has moved on to. `union_flat` and
`union_heap` carry compensation code naming this exact cause: a workaround at the
parent for a bug at the leaf.

Capture `at_eos`/`last_doc_id`/`result.doc_id` before the rewind and restore the
past-the-end position instead of re-seeking. The `rewind` itself has to stay:
`ResumableReader::refresh_pointers` deliberately leaves the buffer pointer stale on
the GC path because a rewind is coming, and `IndexReader::reset` is the only thing
that refreshes `gc_marker`. Skipping the re-seek also skips the re-decode that would
have rebuilt the result's borrowed offsets, so clear them explicitly — we only reach
this after a GC cycle, and `RefreshOutcome::NeedsReseek` documents that an
`RSOffsetSlice` borrowed from the old buffer does not survive one. The resume path
already cleared them ahead of its own cast.

A second defect surfaced from the same `rewind`: on a `Moved`-to-EOF the re-seek
finds nothing, and `last_doc_id()` was left at the 0 the rewind wrote rather than the
document the last yield left. Restored on that arm too, in both functions.

`NotOptimized::revalidate` assigned rather than latched its EOF flag, so a wildcard
that recovered documents could revive it. Latched now, with `past_end` folded into
the guard — `read` recomputes `past_end` from `read_inner`, so latching that alone
would be dropped on the next read. Defensive rather than a reachable bug with today's
wildcards, but `W` also admits `CRQEIterator` wrapping a C implementation.

Why this was not already caught: `revalidate_at_eof` revalidated without dirtying the
index, so `needs_revalidation()` was false and `revalidate` returned before reaching
the `rewind`. The new `revalidate_at_eof_after_gc` removes a document first, across
all five inverted-index leaves, on both the revalidate and resume paths.

`ContractChecker` covers the `RQEIterator` surface but not suspend/resume, so the
cross-cycle guarantees now sit in `revalidate_via_resume`, the single funnel every
resume test goes through.

The union compensation stays: with `QUICK_EXIT` laggers are intrinsic to the mode,
and a full union can still see one from a child that breaks the contract, which
`revalidate_resurrected_child_does_not_drag_a_full_union_backwards` covers
deliberately. Only the comments are retargeted.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@LukeMathWalker
LukeMathWalker force-pushed the luca-eof-terminal-across-revalidate branch from 4c2de26 to 7a56d81 Compare August 11, 2026 08:02
@LukeMathWalker LukeMathWalker changed the title rqe_iterators: keep exhaustion terminal across revalidate and resume [Iterator Revalidation][MOD-17393] Keep exhaustion terminal across revalidate and resume Aug 11, 2026
@github-actions github-actions Bot added size:L and removed size:M labels Aug 11, 2026
Review follow-ups on the exhaustion-is-terminal change:

- Move the contract text onto `RQEIterator::at_eof`, which the citations added
  in core.rs, not_optimized.rs and both unions already pointed at; `revalidate`
  keeps only the re-seek trap, and `resume` links to both.
- A full union now `debug_assert!`s that no child lands behind its position
  instead of quietly recovering: children are covered by flow tests and the disk
  iterators run our suite, so a violation is a bug to surface, not a case to
  absorb. The recovery stays for release builds. The resurrection test asserts
  the panic instead of the compensation.
- Trim the rationale restated at the three restore sites down to what each one
  owns locally.
- Assert that a restored exhausted leaf stays exhausted on a read, on both the
  revalidate and resume paths - the flags and the rewound reader are two
  mechanisms and only a read proves they agree.
- `debug_assert!` that the re-seek's `Moved` arm carries a current, so a `None`
  cannot quietly downgrade it to 'exhausted'.
@LukeMathWalker
LukeMathWalker force-pushed the luca-eof-terminal-across-revalidate branch from f1a7771 to 067d01c Compare August 11, 2026 15:27
- `union_flat::revalidate` justified revalidating exhausted children by
  claiming they may become active again. That is what this branch abolishes:
  they are revalidated because they still hold index references and an aborted
  one has to be removed, not because they can come back.
- The `NotOptimized` latch did not say when `forced_eof` and `wcii.at_eof()`
  disagree. They do whenever `max_doc_id` closed the window with the wildcard
  still live on a document, which `skip_to` sets in two places and the read
  loop in a third - so assigning cleared a flag set for an unrelated reason.
  Name that, and say which of the two guards covers which call.
The existing latch test reaches it through a contract-violating mock, which
left the impression the latch was hardening. It is not: `skip_to` past
`max_doc_id` ends the iterator without touching the wildcard, so a wildcard
that later moves forward under GC - all a conforming one ever does - made the
old assignment clear `forced_eof`, and the next `read` resumed an iterator
that had already reported it was done.

Red before green: with the assignment restored, ContractChecker fails the new
test with 'an exhausted iterator must keep returning None, but it yielded doc
60'.
`forced_eof` and `past_end` recorded overlapping facts, which is what made the
latch hard to read: every site setting `forced_eof` also left `past_end` set,
and where `past_end` was set alone it was because the position had reached
`max_doc_id`, which `has_next`'s second term already catches. So `past_end`
latched - cleared only by `rewind`, as exhaustion being terminal requires -
covers both roles, and `forced_eof` goes.

`read_inner` now owns the invariant that every `Ok(false)` leaves `past_end`
set, so `read`/`skip_to` relay its answer instead of re-deriving one. That also
retires `skip_to`'s `past_end = false`, which could only ever have run when the
flag was already clear.

`OptionalOptimized` has always been built this way; this brings the two
siblings back into line. No behaviour change: breaking the latch still fails
the same two tests, and only those.
@sonarqubecloud

Copy link
Copy Markdown

@LukeMathWalker
LukeMathWalker added this pull request to the merge queue Aug 12, 2026
@LukeMathWalker
LukeMathWalker removed this pull request from the merge queue due to a manual request Aug 12, 2026
@LukeMathWalker
LukeMathWalker added this pull request to the merge queue Aug 13, 2026
Merged via the queue into master with commit ce932d4 Aug 13, 2026
53 checks passed
@LukeMathWalker
LukeMathWalker deleted the luca-eof-terminal-across-revalidate branch August 13, 2026 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants