Skip to content

Harden async stream lifecycle handling - #5

Merged
jssuttles merged 4 commits into
masterfrom
cursor/dcmjs-stream-early-stop-208e
Jul 16, 2026
Merged

jssuttles merged 4 commits into
masterfrom
cursor/dcmjs-stream-early-stop-208e

Conversation

@jssuttles

@jssuttles jssuttles commented Jul 16, 2026 •

Copy link
Copy Markdown

Summary

  • Rejects unsupported and non-cancellable convenience sources before iterator acquisition, preventing hangs, iterator leaks, and unhandled rejections.
  • Distinguishes successful parser completion (stop) from external workflow failure (abort(error)), preserving reasonless, source, cancellation, parser, and late external errors.
  • Fixes untilTag with includeUntilTagValue: true, nested shouldStop scoping, and source failure propagation through availability waits.
  • Closes Node lifecycle races: pending reads are interrupted, teardown is awaited through close or emitClose: false, and synchronous/delayed _destroy errors are preserved.
  • Restores bounded-pump wakeups for every cursor movement path and cancels sources after chunk-processing failures.
  • Clarifies readAheadHighWaterMark, direct readTagHeader() cursor behavior, and stopInfo fields.

Review guide

  • Review src/BufferStream.js for the independent failure/stop signals, serialized finalization, Node/Web adapters, close/error handling, and bounded read-ahead wakeups.
  • Review src/AsyncDicomReader.js to confirm parsing races the pump failure signal, late abort(error) remains effective after source settlement, and direct greater-tag reads rewind consistently.
  • Review the pull-counted regressions in test/readBufferStream.test.js; they assert the next source read is genuinely blocked until cursor advancement.
  • Review lifecycle regressions in test/async-data.test.js for pending Node reads, late abort, reasonless failures, synchronous/delayed teardown errors, and emitClose: false.

Safety

  • readFileFromAsyncStream() accepts reliably cancellable Node streams and Web ReadableStreams.
  • Existing low-level BufferStream.fromAsyncStream() support for generic async iterables remains available and exposes cancellable: false where interruption is not guaranteed.
  • Node successful stop is error-free and awaits actual teardown; external abort uses the supplied error.
  • readAheadHighWaterMark is checked between atomic source chunks, so one source chunk may exceed the threshold.
  • pump.abort(error) rejects pending reads; callers must still await their external processing promise because fulfilled promises cannot be retroactively rejected.

Performance

Interleaved same-process benchmark against dcmjs-org/master (81e7e11) on Node 22.14, built bundles, 11 alternating rounds, test/sample-dicom.dcm (~528 KB):

Operation Upstream median This branch median Difference
Sync read 0.3603 ms 0.3738 ms +3.74%
Sync write 2.3704 ms 2.3688 ms -0.07%
Async full read 0.6327 ms 0.6670 ms +5.42%
Async metadata-only 0.5198 ms 0.5555 ms +6.85%

The largest measured absolute difference is about 0.036 ms per file.

Test plan

  • Full Node 22 suite (360 tests)
  • Full Node 24 suite (360 tests)
  • Pull-counted backpressure tests that fail when limiting is disabled
  • Reasonless/falsy source failure regressions
  • Pending read, late abort, and source settlement races
  • Synchronous/delayed Node teardown errors and emitClose: false
  • Web cancellation after chunk-processing failure
  • Reset, bulk cursor, toEnd(), and concat() wakeups
  • ESLint and Prettier
  • Production Rollup build
  • Interleaved current-vs-upstream performance benchmark
  • Three parallel adversarial reviews plus post-fix verification
Open in Web Open in Cursor 

Reject invalid or non-cancellable convenience sources before pumping, distinguish successful stop from external abort failure, propagate pump errors through availability waits, and correctly stop after included untilTag values. Remove per-tag option normalization work and document the cancellable Node/Web stream contract.
Wake read-ahead pumps at buffer consumption boundaries instead of every byte increment, skip post-tag async checks when no stop callback is configured, and normalize read options only at parser entry points.
Allow late external failures to reject parsing after source settlement, interrupt pending Node reads while awaiting actual teardown, preserve synchronous and delayed close errors, cancel sources after processing failures, and restore bounded-pump wakeups for all cursor movement. Clarify the read-ahead high-water-mark and stop metadata contracts.
Serialize late failures with pump finalization, normalize reasonless source errors, await Node teardown across close-event variants, restore every cursor wakeup path, validate cancellable sources before iterator acquisition, and strengthen backpressure tests so they prove source reads are actually paused.
@jssuttles
jssuttles marked this pull request as ready for review July 16, 2026 16:51
@jssuttles
jssuttles merged commit 3c34b6d into master Jul 16, 2026
9 checks passed
@jssuttles
jssuttles deleted the cursor/dcmjs-stream-early-stop-208e branch July 16, 2026 16:51
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