Skip to content

BUG: segfault on next() after a chunked read_csv chunk raised (C engine) #66622

Description

@jbrockmendel

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import io

import pandas as pd

# column "b" has one non-numeric token, so converting the first chunk raises
data = "a,b\n" + "".join(f"{i},{'oops' if i == 5 else i}\n" for i in range(20))

reader = pd.read_csv(io.StringIO(data), dtype={"b": "int64"}, chunksize=10)

try:
    next(reader)
except ValueError:
    pass  # invalid literal for int() with base 10: 'oops'

next(reader)  # <-- segfault

Issue Description

If converting a chunk raises, the TextFileReader is left in a state where the
next next() / get_chunk() call segfaults instead of raising.

The exception from the first chunk is the expected one:

ValueError: invalid literal for int() with base 10: 'oops'

The second call crashes the interpreter. faulthandler puts it inside the
Cython TextReader.read call:

Fatal Python error: Segmentation fault

Current thread 0x00000001d8f81c40 (most recent call first):
  File ".../pandas/io/parsers/c_parser_wrapper.py", line 234 in read
  File ".../pandas/io/parsers/readers.py", line 1923 in read
  File ".../pandas/io/parsers/readers.py", line 1985 in get_chunk
  File ".../pandas/io/parsers/readers.py", line 1843 in __next__

Scope, from varying the reproducer (identical results on 3.0.5 and 2.3.3):

variant result
engine="c", chunksize=10 segfault
engine="c", iterator=True + get_chunk(10) segfault
engine="c", converters= raising on row 5 instead of dtype= segfault
engine="python", chunksize=10 raises ValueError, no crash
engine="c", no dtype= (first chunk does not raise) fine

So it is specific to the C parser, and what sets it up is that a chunk raised
during conversion — dtype= is just a convenient way to trigger that, and a
raising converters= callable does it too.

Not a regression: reproduces identically on 3.0.5, on 2.3.3, and on a source
build of current main. Nothing string- or pyarrow-specific is involved — the
reproducer has no string column.

Expected Behavior

The second next(reader) should either raise a Python-level exception or return
the remaining rows. Crashing the interpreter is never acceptable, and it is
particularly bad here because catching a per-chunk conversion error and
continuing is a natural pattern for a chunked read.

Installed Versions

Details
INSTALLED VERSIONS
------------------
commit                : e68db09ecf6427d1b62e565bacf17f2e525a3032
python                : 3.13.11
python-bits           : 64
OS                    : Darwin
OS-release            : 23.3.0
Version               : Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:59 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6030
machine               : arm64
processor             : arm
byteorder             : little
LC_ALL                : None
LANG                  : en_US.UTF-8
LOCALE                : en_US.UTF-8

pandas                : 3.0.5
numpy                 : 2.5.1
dateutil              : 2.9.0.post0
pip                   : 25.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO CSVread_csv, to_csvSegfaultNon-Recoverable Error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions