apache / apache/arrow

[Python] RuntimeError when using pyarrow from a thread which is not joined before main exits when pandas is installed

Open
#35,237 4 comments 0 reactions 0 assignees View on GitHub
Component: Python Type: bug
Dominant language
C++
Stars
17.1k
Forks
4.3k
Avg merge
3d 13h
Merged PRs (30d)
88

Description

### Describe the bug, including details regarding any error messages, version, and platform.

This is a relatively straightforward problem in which a thread that is continuing to run during shutdown tries to register an atexit handler.

This only happens if the `pandas` library is installed causing the associated shims to be used. This happens regardless of whether or not pandas is in-use by the application.

The problem can be avoided by making sure to join all theads before main exits, but this is not generally required by python so should be considered a bug.

## Context to reproduce:

### requirements.txt
```
pandas==2.0.0
pyarrow==11.0.0
```

### main.py
```
import threading
import pyarrow

def use_pyarrow() -> None:
table = pyarrow.table({"a": [1, 2, 3]})

def main() -> None:
t = threading.Thread(target=use_pyarrow, args=())
t.start()

if __name__ == "__main__":
main()
```

### Run:
```
$ python main.py
Traceback (most recent call last):
File "pyarrow/pandas-shim.pxi", line 100, in pyarrow.lib._PandasAPIShim._check_import
File "pyarrow/pandas-shim.pxi", line 48, in pyarrow.lib._PandasAPIShim._import_pandas
File "/home/jleibs/pyarrow-repro/venv/lib/python3.10/site-packages/pyarrow/pandas_compat.py", line 24, in
import concurrent.futures.thread # noqa
File "/usr/lib/python3.10/concurrent/futures/thread.py", line 37, in
threading._register_atexit(_python_exit)
File "/usr/lib/python3.10/threading.py", line 1504, in _register_atexit
raise RuntimeError("can't register atexit after shutdown")
RuntimeError: can't register atexit after shutdown
Exception ignored in: 'pyarrow.lib._PandasAPIShim._have_pandas_internal'
Traceback (most recent call last):
File "pyarrow/pandas-shim.pxi", line 100, in pyarrow.lib._PandasAPIShim._check_import
File "pyarrow/pandas-shim.pxi", line 48, in pyarrow.lib._PandasAPIShim._import_pandas
File "/home/jleibs/pyarrow-repro/venv/lib/python3.10/site-packages/pyarrow/pandas_compat.py", line 24, in
import concurrent.futures.thread # noqa
File "/usr/lib/python3.10/concurrent/futures/thread.py", line 37, in
threading._register_atexit(_python_exit)
File "/usr/lib/python3.10/threading.py", line 1504, in _register_atexit
raise RuntimeError("can't register atexit after shutdown")
RuntimeError: can't register atexit after shutdown
```

### Component(s)

Python

Contributor guide

Open the contributing guide

Research direction

Trace the interaction between pyarrow/pandas-shim.pxi, pandas_compat.py, concurrent/futures/thread.py, and threading._register_atexit using the supplied main.py reproduction. Confirm the behavior with pandas 2.0.0 and pyarrow 11.0.0; done means the unjoined worker no longer produces the shutdown RuntimeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data-engineering
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.