Repository navigation
Description
Describe the bug, including details regarding any error messages, version, and platform.
Describe the bug, including details regarding any error messages, version, and platform.
Reading a parquet file by passing a Python file object to pyarrow.parquet.read_table
makes the interpreter hang forever at shutdown on my and other similar machines:
# repro.py - hangs at exit, every run
import pyarrow.parquet as pq
with open("any.parquet", "rb") as f:
pq.read_table(f)
print("done, exiting...") # prints - then the process never exits$ python repro.py
done, exiting...
# hangs forever; SIGKILL required
Passing a path instead of a file object does not hang:
pq.read_table("any.parquet") # exits cleanly, 8/8 runsuse_threads=False also avoids the hang even with a file object (8/8 clean), as does
pa.set_cpu_count(1), so this looks like a CPU thread-pool teardown deadlock that is
only triggered when the read went through a Python file handle (GIL interaction during
pool shutdown?).
How we found it
pandas.read_parquet (which internally opens the file and passes a handle to pyarrow)
was hanging intermittently (~50% of runs when reading 3 files, rarer with 1 file).
Bisecting pandas out of the equation led to the deterministic file-object reproducer
above. So this same bug likely explains "flaky CI hang" reports from pandas users.
Details
- The script body completes and
atexithandlers run; the hang is after that,
during interpreter/C-level finalization.faulthandler(SIGABRT) produces no dump. - The file content doesn't matter - reproduces with a 5-row single-column file.
- Closing the file object (or not) before exit makes no difference.
- A hung process shows the main thread plus ~24 CPU-pool threads all in
futex_do_wait(from/proc/<pid>/task/*/wchan), and ajemalloc_bg_thd:
python(futex_do_wait) jemalloc_bg_thd(futex_do_wait) python(futex_do_wait) x24 ...
Version matrix (same reproducer, 4 runs each; 124 = hung/timeout, 0 = clean exit)
| pyarrow | result |
|---|---|
| 25.0.0 | 124 124 124 124 |
| 24.0.0 | 124 124 124 124 |
| 23.0.1 | 124 124 124 124 |
| 22.0.0 | 124 0 124 124 |
25.0.0 + use_threads=False |
0 0 0 0 |
| 25.0.0 + path instead of file object | 0 0 0 0 |
(22.0.0 being intermittent rather than deterministic suggests a timing-dependent race
that became reliably lost at some point.)
Second interpreter build
Also reproduces on the distro CPython (Python 3.14.4, GCC 15.2.0 build,
/usr/bin/python3.14, fresh venv, pyarrow 25.0.0 wheel) - intermittent there
(3/6 runs hang) instead of deterministic, path-based reads clean 6/6. So this is
not specific to one interpreter build; build/timing differences only shift how
often the race is lost.
Environment
- pyarrow 25.0.0 (manylinux wheel from PyPI, installed via
uv) - Python 3.13.13 (python-build-standalone, Clang 22.1.3) - deterministic hang
- Python 3.14.4 (distro build, GCC 15.2.0) - intermittent hang (~50%)
- Linux 7.0.0-22-generic x86_64, glibc 2.43
- 24 CPUs
- pandas 3.0.3 (only for the original discovery path; not needed for the reproducer)
- Persists after OS upgrade + reboot (kernel 7.0.0-22 → 7.0.0-27, glibc 2.43-2ubuntu2)
Component(s)
Python
Component(s)
Python
Activity
raulcd commentedon Jul 13, 2026
Thank you very much for creating the issue.
I am not sure I understand the
Version matrixsection above, can you help me understand clearly whether this:Is this only happening with a specific parquet file or is it reproducible with a minimal generated parquet file?
mikamikasuki commentedon Oct 2, 2026
I have now independently reproduced this with a generated five-row Parquet file on macOS 15 (arm64), CPython 3.14.7, and the PyArrow 25.0.0 wheel. With 24 CPU threads, the file-object case timed out in 3 of 30 fresh processes; path reads exited cleanly in 12 of 12, and file-object reads with
use_threads=Falseexited cleanly in 12 of 12.I captured native stacks from three hung processes. The main thread was waiting in Arrow's CPU
ThreadPool::Shutdown; a worker was destroying aParquetFileFragment/PyReadableFile, then blocked inPyGILState_Ensure→ CPython thread-state attachment during finalization. This matches CPython's documented behavior that non-finalization threads attempting to attach during late interpreter finalization can be blocked permanently: https://docs.python.org/3.14/c-api/interp-lifecycle.htmlThe source path leads to
OwnedRefNoGILinpython/pyarrow/src/arrow/python/common.h, which reacquires the GIL to decref a retained Python object. I have a local candidate guard for the finalization path, but it still needs a source build and review of the check-to-GIL-acquire race, so I have not opened a PR.AI assistance was used to help trace the source and draft the candidate change; I ran the reproductions and examined the captured stacks. I would like to work on this issue.
deepkilo commentedon Oct 18, 2026
Additional Linux evidence from compatibility testing of a quantitative-research runtime. This was an observed process-exit failure investigated back to native cleanup, not a finding from source review alone. Our reproducer uses generated files and a Python filesystem wrapper; it is related evidence, not a claim that every file-object case has exactly the same ownership path.
With official PyArrow 26, a malformed fragment raises the expected
ArrowInvalid, but the process can still fail during interpreter shutdown. On Linux x86_64 our reduced case aborted on CPython 3.12.15 (12/12 four-fragment runs and 12/12 eight-fragment runs), timed out on 3.13.16 (4/4), and also timed out on unmodified 26 with 3.15.0 (3/3). Upgrading Python alone did not repair it.Self-contained reduced case; run in fresh processes with a timeout, since the symptom is at exit:
Our captured native stack passes through callback-capture destruction,
MergedGenerator/SerialReadahead, the Parquet reader /ReadRangeCache, andPyBuffer::~PyBuffer, thenPyGILState_EnsureduringPy_FinalizeEx. On 3.12 the forced unwind crosses a noexcept boundary and terminates; newer Python parks the late worker while Arrow's pool shutdown joins it.This also relates to #46945: checking
Py_IsInitializedor finalization before acquiring the GIL leaves a check-to-acquire race. A local candidate uses Python 3.15's PEP 788 view/token admission API forPyBuffer,NumPyBuffer, andOwnedRefNoGILcleanup. The complete Python-native wheel was rebuilt, with the eight computational Arrow/Parquet DSOs retained byte-for-byte. Eighteen bounded subprocess checks then exited normally, including malformed-input and valid-input controls; the malformed fragment still raises rather than being suppressed.That candidate is qualified only for main-interpreter, GIL CPython 3.15, not older Python, free-threaded builds or subinterpreters. A failed admission can also mean allocation failure, so the candidate's skipped cleanup may leak a reference in that case. I am sharing the mechanism and verification boundary for discussion, not claiming a universally ready upstream fix or a newly introduced 26 regression.
Disclosure: OpenAI Codex (GPT-6 family; exact variant/effort unavailable in this session) assisted with investigation, tool-driven reproduction, candidate work and this text. The stated runs and native stacks are recorded execution evidence, not a claim that the operator manually performed or reviewed every step. No maintainer ping or competing PR is intended.
apacheGH-50482: [Python] Wait for CPU tasks before interpreter finali…
MagicLex commentedon Oct 22, 2026
The same exit hang also happens when the scan goes through a
pyarrow.fs.PyFileSysteminstead of a Python file object (macOS arm64, CPython 3.12.12).Result on pyarrow 26.0.0, fresh processes, 8 in parallel, hung = printed the count and still alive after 20 s: 15/200 hung with
PyFileSystem, 0/200 withLocalFileSystem.Main thread of a hung process (
sample):PR #54467 likely covers this path; not tested.
Workaround: scan with a native filesystem (
LocalFileSystem, orSubTreeFileSystemover it) instead of a Python-implemented one.gometry: Skip Arrow shutdown-prone subprocess cases temporarily
AndreaBozzo commentedon Oct 27, 2026
An early stop makes this deterministic, and
close()avoids it, but releasing the C stream export does not, becauseExportedArrayStream::Release(cpp/src/arrow/c/bridge.cc) drops itsshared_ptrwithout callingreader->Close().Linux x86_64, PyArrow 26.0.0, CPython 3.12.3, 10 fresh processes each: no close, 10/10 fail at exit (aborts with
PyGILState_Release ... finalizing, or hangs);reader.close(), 10/10 clean; reading throughRecordBatchReader._import_from_c_capsule(reader.__arrow_c_stream__())and closing that, 10/10 fail; then alsoreader.close()on the original, 10/10 clean. Not tested against #54467.fix(databases): scan local Delta tables through a native filesystem s…
daniel-leicht commentedon Nov 11, 2026
I hit the same thing through
pyarrow.fs.PyFileSystem(first seen with deltalake'sDeltaTable.to_pyarrow_table(), which reads through aPyFileSystem). Adding a reproducer that needs only pyarrow (no fsspec, no malformed file) and fails on every run here.The script writes 20 small Parquet files to a temp folder, then starts fresh child processes that each scan them through a
PyFileSystemwhoseopen_input_filereturnspa.PythonFile(open(path, "rb")), and exit right afterto_table().--native-fsis the control (LocalFileSystem).--native-fsterminate called without an active exception)Debian 12, x86_64, glibc 2.36, 72 CPUs, PyPI wheels.
On 3.13.14 with 26.0.1 the stuck worker is still in
PyBuffer::~PyBuffer(pystack, trimmed):and the main thread is in
arrow::internal::ThreadPool::Shutdown(bool)->std::condition_variable::wait, reached from libc at process exit (the libc frames are not symbolized).atexit.register(time.sleep, 0.5)in the child avoids it here. I have not tested #54467.repro.py
AI assistance (Claude) was used for the investigation and to draft this comment.
masatakashiwagi commentedon Nov 19, 2026
Another data point from macOS: the hang is not limited to Python file objects. Wrapping a Python
bytesobject inpa.BufferReadertriggers it too, while the same data in an Arrow-owned buffer does not. It still reproduces on 26.0.1.Environment: macOS 15.7 (arm64, Apple M4, 10 cores), CPython 3.12.9 (python-build-standalone via uv), fresh venv with only the
pyarrowwheel from PyPI.Fresh process per run; a run counts as hung when it printed
okbut was still alive after 6 s.pyarrow 26.0.1, 30 runs each:
use_threads=Trueuse_threads=Falsepa.BufferReader(pa.Buffer)(Arrow-owned)pa.BufferReader(bytes)(Python-owned)io.BytesIO(bytes)python_byteswithuse_threads=Trueon older wheels, 30 runs each: 25.0.0 12/30, 24.0.0 15/30, 23.0.1 25/30, 22.0.0 22/30.In every hung process I sampled (
sample), the main thread is inexit→__cxa_finalize_ranges→arrow::internal::ThreadPool::~ThreadPool()→ThreadPool::Shutdown(bool), waiting on a condition variable. No thread is inPyGILState_Ensure. Forpython_bytes, the only other thread is an idle libdispatch thread, so no CPU-pool worker is left at all. Forbytesio, one worker remains, idle in the worker loop. My guess is that a worker that touched Python during finalization was terminated by CPython without leaving the pool's worker list, soShutdownwaits for it forever. I have not verified this.jasonhimmelstein commentedon Nov 20, 2026
Here's a Linux data point for the
pa.BufferReader(bytes)case: on CPython 3.12 it aborts rather than hangs, even with thePy_IsInitialized()guard from #49943 in place.Environment: pyarrow 26.0.1 (PyPI wheel), CPython 3.12.16 and 3.13.18, Linux 7.0 x86_64, glibc 2.43 (GitHub Actions
ubuntu-latest).Fresh processes, 5 at a time:
terminate called without an active exceptionrawcopied intopa.allocate_buffer(len(raw))firstThe other threads are what make it happen. Without them we couldn't trigger it. In our application, the native threads of another extension module (deltalake's tokio runtime) had the same effect, at about 2% of runs.
Aborting thread (3.12, from a core dump):
The main thread is in
Py_FinalizeEx(pylifecycle.c:1915), inPyGC_Collect. So the last reference to theFileReaderImplis dropped by a future callback on a CPU-pool worker, even withuse_threads=False, afterread_tablehas returned.Py_IsInitialized()is still true that early in finalization, so the guard doesn't stop~PyBufferfrom callingPyGILState_Ensure. CPython 3.12 then ends the worker withpthread_exit, and the forced unwind through thenoexceptdestructor callsstd::terminate. A check onPy_IsFinalizing()(3.13+) or_Py_IsFinalizing()(3.12) would catch it. Releasing the reader on the calling thread beforeread_tablereturns would avoid it altogether.masatakashiwagi commentedon Nov 23, 2026
Thanks, this matches what I see on macOS: the same code path, ending in a hang instead of an abort.
Same repro and environment as my earlier comment (pyarrow 26.0.1, CPython 3.12.9, macOS 15.7 arm64),
use_threads=True. Under lldb with breakpoints onPyThread_exit_threadandpthread_exit, a CPU-pool worker stops inPyThread_exit_threadwhile the main thread is in the GC during finalization, then inpthread_exit. Aftercontinue, the process doesn't abort. It hangs inexit→__cxa_finalize_ranges→ThreadPool::~ThreadPool()→Shutdown.To check that this is the cause of every hang and not just one of them, I also interposed
pthread_exit(DYLD_INSERT_LIBRARIES) to log a backtrace on each call. 15 runs each:pthread_exitcalledpa.BufferReader(bytes)io.BytesIO(bytes)Every hung run had exactly one
pthread_exit. No run that exited normally had any.Where the last reference is dropped depends on the source. Here
read_tablegoes through the dataset scanner:(With
io.BytesIO, I also saw it dropped inPyReadableFile::~PyReadableFile()→OwnedRefNoGIL::~OwnedRefNoGIL().)My reading is that Darwin's
pthread_exitdoesn't unwind C++ frames, so the worker disappears without the pool noticing, andShutdownwaits for it forever. On glibc, the forced unwind through thenoexceptdestructor callsstd::terminateinstead. Either way, it's the same bug.This also explains the idle worker I mentioned for
io.BytesIO. It seems to belong to the IO pool: in a run that didn't hang, the IO pool launched one worker (fromRandomAccessFile::ReadAsync), and the CPU pool, which launched four, was shut down first at exit. So it is just waiting for aShutdownthat never starts, because the main thread is stuck in the CPU pool'sShutdown. I haven't tied that thread to the IO pool directly; this is from the counts and the shutdown order.On the fix: a
Py_IsFinalizing()/_Py_IsFinalizing()check beforePyGILState_Ensurewould catch the cases above. I think it can still race, though, because finalization can begin between the check andtake_gil. Releasing the reader on the calling thread beforeread_tablereturns avoids touching Python from a worker at all, so that seems the more robust option to me.A side note for anyone trying to reproduce: the timing is sensitive to stdout. With stdout redirected to a regular file,
pa.BufferReader(bytes)didn't hang in 15 runs. With stdout sent to/dev/null, it hung in 6–10 of 15.aclerc commentedon Nov 23, 2026
A data point from the most common route into this:
pandas.read_parquet(local_path)on pandas 2.x. pandas 2.x opens a local path with Python'sopen()and passes the handle to pyarrow, so every pandas 2 user reading a local file goes through the Python-file-object path in the original report. We found it as an intermittent hang in a CI job on Python 3.13: a child process that read a parquet cache with pandas never exited.Results from the reproducer below. Each run is a fresh process that calls
pd.read_parquet(path)on a small local file and exits; a run counts as hung if it's still alive after 20 s. Linux x86_64 (Ubuntu 22.04, glibc 2.35), 32 CPUs, Python 3.13.18 (python-build-standalone), fresh venvs with only pandas and pyarrow from PyPI, 12 processes at a time:read_parquet(path)read_parquet(path, filesystem=pyarrow.fs.LocalFileSystem())So on this route the hang is about 9x more frequent on pyarrow 26.0.1 than on 20.0.0, which fits the earlier comments' observation that newer wheels lose the race more often.
pandas 3.1.1 no longer opens a Python handle for a local path (
pandas.io.parquet.get_handleisn't called on a read), so it doesn't reach this bug. On pandas 2.x, passingfilesystem=pyarrow.fs.LocalFileSystem()avoids it the same way.In our application (more native threads loaded: OpenBLAS, numexpr), the same read hung about 1% of the time on both Python 3.13.18 (19 / 2000) and 3.13.16 (40 / 2000), and 0 / 2000 with
LocalFileSystem.gdb on a hung process (pandas 2.3.3, pyarrow 20.0.0) shows the same deadlock as the earlier comments. The main thread:
The CPU-pool worker, dropping the last reference to the dataset scan's reader after
read_parquethad returned:repro.py
Run with
python repro.py --runs 2000 --parallel 12.I haven't tested #54467.
Read parquet through pyarrow's local file system to avoid a hang at e…