#3615·automq

[BUG] Closed StreamReader can restore block state after close

Author: BackendArchitectXCreated Sep 21, 2026Updated Sep 21, 2026

AutoMQ Version

main at 7d4f733348 (3.9.0-SNAPSHOT)

Operating System

Windows local source build. The issue is in StreamReader lifecycle/event-loop logic and is not expected to be OS-specific.

Installation Method

source

Other Relevant Software

  • JDK 17
  • Gradle 8.8
  • Scala 2.13.14

What Went Wrong?

StreamReader.close() clears the reader's block state, but asynchronous block-index/readahead work can cross the close lifecycle boundary.

There are two independently reproducible cases:

  1. A block-index load starts before close() and completes afterward. The asynchronous continuation can restore blocks after the reader has already been closed.
  2. Readahead starts after close(). Without an explicit closed-state guard, it can start a new ObjectManager#getObjects(...) metadata load.

This violates the expected lifecycle invariant that a closed StreamReader must not restore or start loading block-index state.

What Should Have Happened Instead?

After StreamReader.close():

  • work that started before close must not restore block state;
  • queued or later readahead must not start another block-index metadata load;
  • blocksMap must remain empty.

Steps to Reproduce

In-flight load

  1. Start readahead while delaying ObjectManager#getObjects.
  2. Close the reader before the metadata future completes.
  3. Confirm that blocksMap is empty immediately after close.
  4. Complete the delayed metadata load.
  5. Wait for the readahead continuation.
  6. Observe that blocksMap becomes non-empty again.

Post-close load

  1. Close the StreamReader.
  2. Trigger readahead.
  3. Observe that ObjectManager#getObjects(...) is invoked after close.

Both cases are covered by deterministic regression tests.

Additional Information

On current main (7d4f733348):

  • testClosePreventsInflightReadaheadFromRestoringBlocks fails because block state is restored after close.
  • testClosedReaderDoesNotStartNewReadaheadLoad fails because ObjectManager#getObjects(...) is invoked after close.

Applying the fix proposed in #3555 makes both regressions pass.

Proposed fix: #3555