JustinBinber opened a new issue, #41669:
URL: https://github.com/apache/beam/issues/41669

   ### What happened?
   
   Reading concatenated bzip2, lzma or zstd members fails when a member ends 
exactly at a compressed input-read boundary. The completed decoder has 
`eof=True` and empty `unused_data`, so the next input chunk is sent to the same 
finished decoder.
   
   Reproduction on current master (`cd22ba1e017cc296dbf9e42457f5e8bd69fa3034`, 
Python 3.11.7, Windows):
   
   ```python
   import bz2
   import os
   import tempfile
   from apache_beam.io.filesystem import CompressedFile, CompressionTypes
   
   first = bz2.compress(b"first row\n")
   second = bz2.compress(b"last row\n")
   with tempfile.TemporaryDirectory() as directory:
       path = os.path.join(directory, "input.bz2")
       with open(path, "wb") as output:
           output.write(first + second)
       with open(path, "rb") as raw:
           with CompressedFile(raw, CompressionTypes.BZIP2,
                               read_size=len(first)) as source:
               print(source.read(100))
   ```
   
   Actual: `EOFError: End of stream already reached`. Expected: `b'first 
row\nlast row\n'`.
   
   The same alignment raises `EOFError: Already at end of stream` for lzma and 
`ZstdError: cannot use a decompressobj multiple times` for zstd. It also 
affects `readline()` and empty members. Gzip and deflate are useful controls 
and continue to read these inputs.
   
   This can occur with the default read size too: a real zstd first frame of 
exactly 16,777,216 compressed bytes followed by another frame raises through 
both `FileSystems.open(...).read(n)` and `fileio.ReadableFile.read()`. The same 
valid file decodes to 16,776,835 bytes with `zstandard`'s stream reader. This 
reproduction uses zstandard 0.25.0 and does not mock the filesystem or the 
decompressor.
   
   `_fetch_to_internal_buffer` currently creates a new decoder only when 
`unused_data` is nonempty. I plan to restart an EOF decoder when another 
nonempty chunk is ready, whether that chunk comes from `unused_data` or the 
next raw read, with five-codec regression coverage. This is independent of the 
unlimited-size behavior in #41621 / #41622; a positive bounded read reproduces 
it.
   
   Related: #22939 concerns an assertion in the older zstandard 0.18.0 debug 
implementation. This report reproduces finished-decoder reuse with released 
libraries and does not claim to reproduce or fix that separate debug assertion.
   
   ### Issue Priority
   
   Priority: 2 (default / most bugs should be filed as P2)
   
   ### Issue Components
   
   - [x] Component: Python SDK
   - [x] Component: IO connector
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to