{"record":{"id":"bf1efbcc628c4542","repo":"apache/beam","slug":"decompressor-not-initialized","errorCode":null,"errorMessage":"decompressor not initialized","messagePattern":"decompressor not initialized","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/filesystem.py","lineNumber":294,"sourceCode":"          # fully decompressing files.\n          self._read_buffer.write(self._decompressor.flush())\n\n        # Record that we have hit the end of file, so we won't unnecessarily\n        # repeat the completeness verification step above.\n        self._read_eof = True\n\n  def _read_from_internal_buffer(self, read_fn):\n    \"\"\"Read from the internal buffer by using the supplied read_fn.\"\"\"\n    self._read_buffer.seek(self._read_position)\n    result = read_fn()\n    self._read_position += len(result)\n    self._uncompressed_position += len(result)\n    self._read_buffer.seek(0, os.SEEK_END)  # Allow future writes.\n    return result\n\n  def read(self, num_bytes: Optional[int] = DEFAULT_READ_BUFFER_SIZE) -> bytes:\n    if not self._decompressor:\n      raise ValueError('decompressor not initialized')\n\n    self._fetch_to_internal_buffer(num_bytes)\n    return self._read_from_internal_buffer(\n        lambda: self._read_buffer.read(num_bytes))\n\n  def readline(self) -> bytes:\n    \"\"\"Equivalent to standard file.readline(). Same return conventions apply.\"\"\"\n    if not self._decompressor:\n      raise ValueError('decompressor not initialized')\n\n    bytes_io = io.BytesIO()\n    while True:\n      # Ensure that the internal buffer has at least half the read_size. Going\n      # with half the _read_size (as opposed to a full _read_size) to ensure\n      # that actual fetches are more evenly spread out, as opposed to having 2\n      # consecutive reads at the beginning of a read.\n      self._fetch_to_internal_buffer(self._read_size // 2)\n      line = self._read_from_internal_buffer(","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/filesystem.py#L276-L312","documentation":"CompressedFile lazily creates its decompressor only when opened for reading; read() requires it. Calling read on a CompressedFile that was opened for writing (or whose decompressor was never initialized) raises ValueError, since there is no codec to decode the stream.","triggerScenarios":"Opening a file in write mode ('wb') and then calling read(); invoking seek() on a write-mode CompressedFile, which internally calls read().","commonSituations":"Mixing read/write on the same CompressedFile handle; calling seek (documented to rely on read) on a writer; swapping reader/writer objects by mistake.","solutions":["Open a separate CompressedFile in read mode ('rb') for reading instead of reusing the writer.","Guard reads with readable() and avoid seek() on write-mode handles.","Fix the mixed read/write code path — CompressedFile does not support dual-mode use."],"exampleFix":"# before\nfh = open(path, 'wb')\nf = CompressedFile(fh, compression_type=CompressionTypes.GZIP)\nf.seek(0)  # internally reads -> raises\n# after\nf = CompressedFile(open(path, 'rb'), compression_type=CompressionTypes.GZIP)\nf.seek(0)","handlingStrategy":"type-guard","validationCode":"if not compressed_file.readable():\n    raise ValueError('CompressedFile opened in write mode; cannot read')","typeGuard":"def can_read(f) -> bool:\n    return hasattr(f, 'readable') and f.readable()","tryCatchPattern":"try:\n    data = f.read(n)\nexcept ValueError as e:\n    if 'decompressor not initialized' in str(e):\n        raise IOError('attempted read on write-mode CompressedFile') from e\n    raise","preventionTips":["Do not call seek() or read() on write-mode CompressedFile handles.","Open separate handles for reading and writing the same compressed file."],"tags":["python","apache-beam","io","invalid-state"],"backgroundTag":"invalid-state-transition","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}