masatakashiwagi commented on issue #50482: URL: https://github.com/apache/arrow/issues/50482#issuecomment-7098947606
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 on `PyThread_exit_thread` and `pthread_exit`, a CPU-pool worker stops in `PyThread_exit_thread` while the main thread is in the GC during finalization, then in `pthread_exit`. After `continue`, the process doesn't abort. It hangs in `exit` → `__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: | source | hung | `pthread_exit` called | both | | --- | --- | --- | --- | | `pa.BufferReader(bytes)` | 7/15 | 7/15 | 7 | | `io.BytesIO(bytes)` | 4/15 | 4/15 | 4 | 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_table` goes through the dataset scanner: ``` # pa.BufferReader(bytes) PyThread_exit_thread take_gil PyEval_RestoreThread PyGILState_Ensure arrow::py::PyBuffer::~PyBuffer() arrow::io::BufferReader::~BufferReader() arrow::dataset::FileSource::FileSource(...)::$_0 (destructor) arrow::dataset::FileFragment::~FileFragment() ... arrow::internal::ThreadPool::SpawnReal(...) (CPU pool worker) # io.BytesIO(bytes): a PyBuffer held in the pre-buffer cache ... PyGILState_Ensure arrow::py::PyBuffer::~PyBuffer() arrow::ConcreteFutureImpl::~ConcreteFutureImpl() arrow::io::internal::ReadRangeCache::~ReadRangeCache() parquet::SerializedFile::~SerializedFile() parquet::arrow::FileReaderImpl::~FileReaderImpl() ... arrow::internal::ThreadPool::SpawnReal(...) (CPU pool worker) ``` (With `io.BytesIO`, I also saw it dropped in `PyReadableFile::~PyReadableFile()` → `OwnedRefNoGIL::~OwnedRefNoGIL()`.) My reading is that Darwin's `pthread_exit` doesn't unwind C++ frames, so the worker disappears without the pool noticing, and `Shutdown` waits for it forever. On glibc, the forced unwind through the `noexcept` destructor calls `std::terminate` instead. 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 (from `RandomAccessFile::ReadAsync`), and the CPU pool, which launched four, was shut down first at exit. So it is just waiting for a `Shutdown` that never starts, because the main thread is stuck in the CPU pool's `Shutdown`. 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 before `PyGILState_Ensure` would catch the cases above. I think it can still race, though, because finalization can begin between the check and `take_gil`. Releasing the reader on the calling thread before `read_table` returns 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. -- 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]
