Fix StreamReader#close and align #read with Ruby's IO conventions - #151
Fix StreamReader#close and align #read with Ruby's IO conventions#151sribalakumar wants to merge 1 commit into
Conversation
787c4e4 to
c260dd7
Compare
173ac75 to
82f8165
Compare
|
@SpringMT @Watson1978 — thanks for all the work on this gem! Since this is my first PR here, GitHub is holding the CI workflow runs pending approval (the standard first-time-contributor gate), so the checks show as empty rather than failing. Would one of you be able to approve the pending runs when you get a chance? The full suite passes locally (88 examples, 0 failures), so hopefully it won't cost much CI time. On the open question in #149 — whether changing |
82f8165 to
04dc46d
Compare
StreamReader#read passed its length argument straight to the underlying IO, so it read that many *compressed* bytes and returned however many decompressed bytes fell out. The size of the return value tracked the compression ratio rather than the caller's request: a small frame could return far more than asked for, while a larger one returned an empty String because zstd was still filling an internal block. That makes the reader unusable for consumers that need a specific number of bytes, such as Gem::Package::TarReader. Buffer decompressed output so length means decompressed bytes, and serve reads from that buffer. Refills use decompress_with_pos rather than decompress: it writes at most ZSTD_DStreamOutSize bytes per call and reports how much input it consumed, so a high compression ratio cannot balloon the buffer. Also return nil at EOF instead of raising StandardError, matching IO#read. Rescuing StandardError to detect EOF would otherwise swallow genuine decompression failures, which the extension raises as RuntimeError. Add eof?, support read with no length and an outbuf argument, and fix close, which called finish on StreamingDecompress (a method it does not define) and wrote to a read-only IO. Correct the spelling of the experimental marker on both StreamReader and StreamWriter. The marker stays in place: this does not promote either class to a stable API.
04dc46d to
2304872
Compare
|
@SpringMT I addressed pipeline failures noticed with Ruby 2.7 syntaxError. Can you reapprove the workflows so a new pipeline is run. |
Fixes #149.
Opening this as a concrete proposal — happy to close it if you'd prefer the non-breaking route I mentioned in the issue (leave
StreamReaderas-is, add a separate IO-conformant class instead). No hard feelings either way.What changed
Two bugs here, plus one convention fix:
StreamReader#readpassedlengthstraight through to the underlying IO, so it read that many compressed bytes and returned however many decompressed bytes fell out. The return size tracked the compression ratio instead of the caller's request.closeraisedNoMethodErroron every call — it tried to call@stream.finish, whichStreamingDecompressdoesn't define, and wrote to@io, which is only open for reading.This patch buffers decompressed output and serves reads from that buffer, so
lengthmeans decompressed bytes, and fixescloseto just close the IO.read(512), 44-byte frameread(512)x4, 20 KB frame[0, 0, 0, 0][512, 512, 512, 512]StandardErrornilcloseNoMethodErrorAlso adds
eof?,readwith no length, and anoutbufargument.Why
stream_writer.rbis in this diffIt's a one-character change, not a behaviour change: the
@todo Exprimentalmarker was misspelled, so this fixes it to@todo Experimentalon bothStreamReaderandStreamWriter. The marker itself is deliberately kept onStreamReader— this PR doesn't promote it to a stable API, it stays experimental.On
decompress_with_posRefills use
decompress_with_posrather thandecompress.decompressloops until all input is consumed, so output per call is unbounded — with a high compression ratio a 64 KB read can expand to hundreds of MB in the buffer.decompress_with_poswrites at mostZSTD_DStreamOutSizebytes per call and reports how much input it consumed, which keeps the buffer bounded regardless of ratio. That matters when the compressed input isn't trusted.Measured on a 14.3 MB archive (64x ratio), peak buffer:
decompress4.6 MB ->decompress_with_pos0.57 MB.Breaking change
read's semantics change, so this is breaking for anyone depending on the current behaviour. The existing spec's expectations change accordingly:readalso becomes arity-optional to matchIO#read.Tests
Full suite passes — 88 examples, 0 failures (was 69). New coverage: exact-length reads across block boundaries, ratio independence,
nilat EOF,readwith no args,read(0), negative length,outbuf(including the EOF case),eof?,close, customchunk_size, round-trip integrity for both compressible and incompressible input, and an end-to-end test drivingGem::Package::TarReaderthrough the reader.