Skip to content

fix(range-reader): serve reads that arrive as their stream finishes - #861

Merged
kylebarron merged 2 commits into
mainfrom
kyle/range-reader-settled-stream
Nov 6, 2026
Merged

kylebarron merged 2 commits into
mainfrom
kyle/range-reader-settled-stream

Conversation

@kylebarron

Copy link
Copy Markdown
Member

Note

This PR was written by Claude (Claude Code), not by @kylebarron.

A read for bytes that a RangeStream had just finished receiving could miss both the stream and the byte cache, and go out as a new request. For COGs, that fetched a header block twice. This closes both windows that #826 describes.

Closes #826

Changes

  • RangeStream.attach serves a finished stream's bytes. settle() marks a stream inactive as soon as its body ends, but the reader keeps it in inflight, and stores its bytes in the cache, only once run() resolves. A read arriving in between found the stream and was refused. Now a stream that is done resolves any read its received bytes cover. A read past them is still refused, because a stream that is done never receives more.
  • RangeReader reads the cache again before queueing a read when a stream finished while the read's first lookup was pending, since that lookup may have run before the stream stored its bytes. A read that misses with no stream finished in the meantime is queued after one lookup, as before.

Tests

New tests, each failing on main:

  • RangeReader, "sends one request when a read arrives as the stream holding its bytes finishes": the issue's repro. A 64 KiB metadata block arrives in one chunk, and the next read is issued from the first one's continuation. Main sends 2 requests.
  • RangeReader, "looks in the cache again when the stream holding a read finishes during its lookup": a cache whose lookup answers after the stream has stored its bytes and left. Main sends 2 requests.
  • RangeStream, "serves only received bytes once cancelled": a cancelled stream serves a read inside the prefix it received, and refuses one past it. Main refuses both.
  • RangeStream, "resolves an attached member when its bytes pass, at once when they already have" now expects a finished stream to serve a read it has the bytes for. Its last assertion said the opposite, which is the bug.

Mutation checks, undoing one part of the change at a time:

Mutation Tests that fail
attach refuses every read once the stream is done 3: both RangeStream tests and the repro
No second cache lookup 1: the lookup test
attach takes any read once the stream is done 1: "serves only received bytes once cancelled"

The issue's Node repro, run against the built dist:

main:
1 chunk(s): 2 requests [ 'bytes=0-65535', 'bytes=0-65535' ]
2 chunk(s): 1 requests [ 'bytes=0-65535' ]
4 chunk(s): 1 requests [ 'bytes=0-65535' ]

this branch:
1 chunk(s): 1 requests [ 'bytes=0-65535' ]
2 chunk(s): 1 requests [ 'bytes=0-65535' ]
4 chunk(s): 1 requests [ 'bytes=0-65535' ]

The range-reader, geotiff, deck.gl-raster and deck.gl-geotiff tests pass, pnpm -r --filter='!docs' typecheck passes, and Biome is clean.

Notes

  • The count of finished streams is reader-wide, not per file. A read looks in the cache a second time only when some stream finished during its first lookup. With the default in-memory cache that is rare and cheap, and with a slower persistent ByteCache it doesn't double the lookups of every cold read.
  • The first cache lookup stays inline in lookup. Moving it into an async helper delayed every read's attach by two microtasks.

🤖 Written by Claude Code

kylebarron and others added 2 commits November 6, 2026 11:50
A finished stream serves reads its received bytes cover while the reader
still holds it, and a lookup reads the cache again when a stream finished
while it waited, so neither window sends the bytes again. See #826.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
So a read reaches the in-flight streams as soon as it did before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kylebarron kylebarron added this to the 0.10 milestone Nov 6, 2026
@github-actions github-actions Bot added the fix label Nov 6, 2026
@kylebarron
kylebarron merged commit d33d28f into main Nov 6, 2026
3 checks passed
@kylebarron
kylebarron deleted the kyle/range-reader-settled-stream branch November 6, 2026 16:59
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.

RangeReader sends a read again when it arrives as the stream holding its bytes finishes

1 participant