Skip to content

LZ4FrameFile.flush() flushes the underlying file in read mode, so Python 3.13 prints "I/O operation on closed file" at garbage collection #351

Open

Description

@bsauvage1

On Python 3.13, an io.BufferedReader wrapped around a read-mode LZ4FrameFile prints an ignored exception when it is garbage-collected after the underlying file has been closed. Python 3.12 is silent on the same code.

Minimal repro (lz4 4.4.5):

import gc, io, lz4.frame

with open("t.lz4", "wb") as f:
    f.write(lz4.frame.compress(b"x" * 100))

with open("t.lz4", "rb") as f:
    b = io.BufferedReader(lz4.frame.LZ4FrameFile(f, "rb"))
    b.read()

del b
gc.collect()

Output on 3.13.18 (nothing on 3.12.16):

Exception ignored in: <_io.BufferedReader>
Traceback (most recent call last):
  File ".../lz4/frame/__init__.py", line 753, in flush
    self._fp.flush()
ValueError: I/O operation on closed file.

LZ4FrameFile.flush() only writes compressor output when the file is writable, but it always calls self._fp.flush(). In read mode there is nothing to flush.

Suggested fix: return early unless the file is writable, so read mode never touches _fp:

def flush(self):
    if not self.writable():
        return
    if self._compressor.has_context():
        self._fp.write(self._compressor.flush())
    self._fp.flush()

joblib hits this on every lz4 joblib.load, because it wraps LZ4FrameFile in a BufferedReader it does not close (I will open a matching joblib issue).

Activity

  1. bsauvage1 commented on Oct 30, 2026

    @bsauvage1
    Author

    Matching joblib issue: joblib/joblib#1929

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions