#15255·rocksdb

MultiScan: ~ScanIterator() asserts on the status operator++ just threw, so reaching max_prefetch_size aborts instead of raising MultiScanException

Author: hanke580Created Sep 23, 2026Updated Sep 23, 2026

Summary

MultiScanArgs::max_prefetch_size is documented as a limit the caller opts into, whose documented outcome is Status::PrefetchLimitReached() surfaced as a MultiScanException. When the limit is actually reached mid-scan, Scan::ScanIterator::operator++ records the non-OK status and throws — and the throw unwinds through ~ScanIterator(), which assert(false)s on exactly that status. In any build without -DNDEBUG the process aborts inside the unwind, so the catch block the header documents never runs.

The same shape exists at both iterator levels, so a fix has to touch both.

The documented contract

include/rocksdb/options.h, MultiScanArgs:

cpp
  // Maximum size (in bytes) for the data blocks loaded by a MultiScan.
  // This limits the amount of I/O and memory usage by pinned data blocks.
  //
  // When set to 0 (the default), there is no limit. When the limit is reached,
  // the iterator will start returning Status::PrefetchLimitReached().
  uint64_t max_prefetch_size = 0;

and the handling pattern documented at the top of include/rocksdb/multi_scan.h:

cpp
//  try {
//    for (auto scan : *iter) {
//      for (auto it : scan) { ... }
//    }
//  } catch (MultiScanException& ex) {
//    // Check ex.status()
//  } catch (std::logic_error& ex) {
//    // Check ex.what()
//  }

So reaching the limit is an expected outcome the caller asked for and handles by catching MultiScanException.

Root cause

include/rocksdb/multi_scan.h, Scan::ScanIterator:

cpp
    ~ScanIterator() {
      if (!status_.ok()) {
        fprintf(stderr, "ScanIterator status: %s\n",
                status_.ToString().c_str());
        assert(false);
      }
    }

    ScanIterator& operator++() {
      ...
        status_ = db_iter_->status();
        if (!status_.ok()) {
          throw MultiScanException(status_);
        }
      ...
    }

operator++ guarantees status_ is non-OK on every throw path; the destructor treats that as impossible. The MultiScanException then unwinds through it.

The outer iterator repeats the pattern. include/rocksdb/multi_scan.h:

cpp
    ~MultiScanIterator() { assert(status_.ok()); }

db/multi_scan.cc:

cpp
MultiScanIterator& MultiScanIterator::operator++() {
  status_ = db_iter_->status();
  if (!status_.ok()) {
    throw MultiScanException(status_);
  }

The outer one has no fprintf, so it aborts silently. (MultiScanIterator's constructor also throws with status_ set, but that path is safe — an object whose constructor throws is never destroyed.)

Reproduction

The probe runs exactly the documented try/catch pattern over six scan ranges of a 60,000-key DB, taking max_prefetch_size as its argument.

Assertions enabled (no -DNDEBUG):

  max_prefetch=0          rc=0    keys=9240  completed without error
  max_prefetch=67108864   rc=0    keys=9240  completed without error
  max_prefetch=1048576    rc=0    keys=9240  completed without error
  max_prefetch=65536      rc=134  ScanIterator status: Result incomplete: MultiScan reached file prefetch limit
  max_prefetch=4096       rc=134  ScanIterator status: Result incomplete: MultiScan reached file prefetch limit
  max_prefetch=1024       rc=0    keys=0     caught MultiScanException: ... reached file prefetch limit
  max_prefetch=1          rc=0    keys=0     caught MultiScanException: ... reached file prefetch limit

rc=134 is SIGABRT. The same source with -DNDEBUG:

  max_prefetch=0        rc=0  keys=9240  completed without error
  max_prefetch=65536    rc=0  keys=15    caught MultiScanException: ... reached file prefetch limit
                              (preceded by "ScanIterator status: ..." on stderr)
  max_prefetch=4096     rc=0  keys=8     caught MultiScanException: ... reached file prefetch limit
  max_prefetch=1024     rc=0  keys=0     caught MultiScanException: ... reached file prefetch limit
  max_prefetch=1        rc=0  keys=0     caught MultiScanException: ... reached file prefetch limit

Two things to read out of this.

The abort needs the limit to be crossed mid-scan. At the smallest limits the exception comes from a path that never leaves a ScanIterator holding a bad status, so those cases are handled cleanly. The aborting window is the one where some keys are produced first — which is the ordinary case for any realistic limit. (The exact byte values bounding that window depend on the build's block sizes and compression settings; the build above has all compression disabled. What is stable is the shape: limits large enough to finish are fine, limits small enough to fail before the first key are fine, and everything between aborts.)

The release behaviour is also wrong. With NDEBUG the assert disappears but the fprintf does not. A public library header writes an unconditional diagnostic to stderr every time a scan iterator is destroyed holding an error status — not through the configured Logger, not suppressible, and on a path the application is already handling correctly by catching the exception.

Suggested fix

The destructors' assumption is simply wrong — operator++ sets status_ and throws precisely so the caller can inspect it. Removing the bodies is sufficient:

cpp
~ScanIterator() = default;
~MultiScanIterator() = default;

If a diagnostic is wanted for genuinely unexpected statuses it belongs behind the DB's Logger, and it must exclude the statuses the API documents as normal outcomes — PrefetchLimitReached above all, since it only occurs because the caller asked for it.

Environment

  • main at 507206e46bdf876174532b8eb6b26f54f698b8dc (2026-09-21)
  • CMake Release, gcc 11.4.0, Ubuntu 22.04 x86-64; probe built with and without -DNDEBUG
  • Also present in 11.8.1

Prior art

Three merged PRs have already removed asserts from MultiScan paths that can hold a non-OK status, all for async-read failures:

  • #14077 Fix multiscan assert failure in stress test (2025-10-24)
  • #14171 Surface MultiScan async read failure instead of asserting (2025-12-04)
  • #14244 Don't assert on async_read.status in MultiScan code path (2026-01-16)

None of them touches the two destructors above, which are unchanged on main. The difference worth noting is that those three concern I/O failures, whereas this status is produced by the library on purpose because the caller set an option asking for it.

No issue or HISTORY.md entry describes this one. Re-run the searches immediately before submitting.