aclerc commented on issue #50482:
URL: https://github.com/apache/arrow/issues/50482#issuecomment-7101782314

   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's `open()` 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:
   
   | pandas | pyarrow | `read_parquet(path)` | `read_parquet(path, 
filesystem=pyarrow.fs.LocalFileSystem())` |
   |---|---|---|---|
   | 2.3.3 | 20.0.0 | 14 / 2000 hung | 0 / 2000 |
   | 2.3.3 | 26.0.1 | 123 / 2000 hung | not run |
   | 3.1.1 | 20.0.0 | 0 / 2000 | not run |
   | 3.1.1 | 26.0.1 | 0 / 2000 | not run |
   
   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_handle` isn't called on a read), so it doesn't reach 
this bug. On pandas 2.x, passing `filesystem=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:
   
   ```
   arrow::internal::ThreadPool::Shutdown(bool)
   std::_Sp_counted_ptr<arrow::internal::ThreadPool*, ...>::_M_dispose()
   __run_exit_handlers
   exit
   __libc_start_call_main
   ```
   
   The CPU-pool worker, dropping the last reference to the dataset scan's 
reader after `read_parquet` had returned:
   
   ```
   __libc_pause
   PyThread_hang_thread
   take_gil
   _PyThreadState_Attach
   PyGILState_Ensure
   arrow::py::PyBuffer::~PyBuffer()
   
arrow::Future<std::shared_ptr<arrow::Buffer>>::SetResult(...)::{lambda(void*)#1}
   arrow::ConcreteFutureImpl::~ConcreteFutureImpl()
   arrow::io::internal::ReadRangeCache::LazyImpl::~LazyImpl()
   parquet::ParquetFileReader::~ParquetFileReader()
   parquet::arrow::(anonymous namespace)::FileReaderImpl::~FileReaderImpl()
   ... MergedGenerator / SlicingGenerator / SerialReadaheadGenerator state 
destructors
   ```
   
   <details>
   <summary>repro.py</summary>
   
   ```python
   """Hang at interpreter exit after pandas.read_parquet(local_path), Python 
3.13+.
   
   Each run is a fresh child process that reads a small local parquet file with
   pandas.read_parquet(path) and exits. pandas 2.x opens the path as a Python 
file handle,
   so pyarrow's reads are held in arrow::py::PyBuffer.
   
       python repro.py                # pandas default: some runs hang at exit
       python repro.py --native-fs    # control: 
filesystem=pyarrow.fs.LocalFileSystem()
   """
   
   import argparse
   import subprocess
   import sys
   import tempfile
   from pathlib import Path
   
   import numpy as np
   import pandas as pd
   import pyarrow as pa
   
   
   def child(path: str, *, native_fs: bool) -> None:
       kwargs = {}
       if native_fs:
           import pyarrow.fs
   
           kwargs["filesystem"] = pyarrow.fs.LocalFileSystem()
       print(len(pd.read_parquet(path, **kwargs)), flush=True)
   
   
   def main() -> None:
       parser = argparse.ArgumentParser()
       parser.add_argument("--runs", type=int, default=200)
       parser.add_argument("--parallel", type=int, default=8)
       parser.add_argument("--timeout", type=float, default=20)
       parser.add_argument("--native-fs", action="store_true")
       parser.add_argument("--child", help=argparse.SUPPRESS)
       args = parser.parse_args()
       if args.child:
           child(args.child, native_fs=args.native_fs)
           return
   
       print(
           f"Python {sys.version.split()[0]}, pandas {pd.__version__}, pyarrow 
{pa.__version__}, "
           f"{pa.cpu_count()} CPU threads; {'LocalFileSystem' if args.native_fs 
else 'pandas default'}"
       )
       with tempfile.TemporaryDirectory() as tmp:
           path = str(Path(tmp) / "data.parquet")
           idx = pd.date_range("2024-03-01", periods=1440, freq="60s", tz="UTC")
           pd.DataFrame({f"c{i}": np.random.default_rng(i).random(len(idx)) for 
i in range(8)}, index=idx).to_parquet(path)
           cmd = [sys.executable, __file__, "--child", path, *(["--native-fs"] 
if args.native_fs else [])]
           hung = 0
           for start in range(0, args.runs, args.parallel):
               procs = [
                   subprocess.Popen(cmd, stdout=subprocess.DEVNULL, 
stderr=subprocess.DEVNULL)
                   for _ in range(min(args.parallel, args.runs - start))
               ]
               for p in procs:
                   try:
                       p.wait(timeout=args.timeout)
                   except subprocess.TimeoutExpired:
                       hung += 1
                       p.kill()
                       p.wait()
       print(f"{hung} of {args.runs} runs hung at exit")
   
   
   if __name__ == "__main__":
       main()
   ```
   
   Run with `python repro.py --runs 2000 --parallel 12`.
   
   </details>
   
   I haven't tested #54467.
   
   AI assistance (Claude) was used for the investigation and to draft this 
comment.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to