Skip to content

CSV input streams remain open after parsing errors #950

Open

Description

@IlyaKhalafi

CSV iterators from rows_from_file() close their input stream on normal exhaustion but leave it open when parsing fails. Holding the iterator after a failed import retains the TextIOWrapper and input descriptor.

import io
from sqlite_utils.utils import Format, RowError, rows_from_file
stream = io.BytesIO(b'name\nCleo,extra\n')
rows, _ = rows_from_file(stream, format=Format.CSV)
try:
    next(iter(rows))
except RowError:
    pass
print(stream.closed)

This prints False. The iterator cannot produce further rows after its generator raises RowError, so it should release the stream just as it does at end-of-input. Invalid UTF-8 input similarly leaves the file open after UnicodeDecodeError.

Reproduced on current main with CPython 3.14.6. Two tests using real files fail because the descriptor remains open after the expected parsing exception. _CloseableIterator.__next__() only closes the owned text wrapper when it catches StopIteration.

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions