Prevent deadlocks in `ctypes.util.dllist()` on Linux (and maybe other Unices)
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
On Linux, ctypes.util.dllist() uses dl_iterate_phdr, which due to the way it works is prone to deadlocks when used from Python. It would be good if Python guaranteed that this form of deadlock won't happen. Update: See reproducer below, in the comments.
This is relevant to threadpoolctl (183 million PyPI downloads last month), which has started to use ctypes.util.dlllist() in its latest release, and in an ideal world would only rely on it once Python 3.14 is the oldest supported release.
Background: A real deadlock bug involving dl_iterate_phdr I encountered in the real world
In older releases, threadpoolctl would use dl_iterate_phdr itself (using ctypes). Importantly, this C API uses callbacks, and so threadpoolctl had the callbacks call back into Python.
Meanwhile, NumPy on Linux also calls dl_iterate_phdr indirectly (via backtrace API on Linux, some sort of temporary array optimization). NumPy holds the GIL while doing so (I will file an issue about that too).
threadpoolctl is very often used together with NumPy, so now that threading is becoming more commonly used, this interaction is a likely scenario. And presumably other libraries use dl_iterate_phdr, either directly, or indirectly as NumPy does.
So here's the scenario:
- Thread A is calling
dl_iterate_phdrviathreadpoolctl. - Thread B is calling
dl_iterate_phdrvia NumPy.
The following sequence of events can happen:
- Thread A calls
dl_iterate_phdr. This acquires an internal lock inside thedlsystem; we'll call this DL-LOCK. - Thread A calls into Python, and the GIL gets released inside that callback.
- Thread B gets scheduled, it holds the GIL.
- Thread B calls
dl_iterate_phdr, which tries to acquireDL-LOCK... but it's held by thread A. Meanwhile, thread A is waiting for the GIL, which is held by thread B.
At this point the program is deadlocked, and no Python thread will ever run again.
ctypes.util.dllist() in CPython seems potentially vulnerable to this deadlock
Looking at the relevant code in CPython, the same deadlock could happen if PyUnicode_DecodeFSDefault or PyList_Append release the GIL.
The former at least ends up dealing with the whole codec lookup system, plus some specific filesystem encoding infrastructure, so what it does is pretty complex. It might release the GIL, or it might not. And it might potentially change to not be OK in future versions.
The solution
When using dl_iterate_phdr, dllist() should have as part of its contract that the GIL can't be released and reacquired while calling dl_iterate_phdr.
More concretely:
- Not call into Python at all in the
dl_iterate_phdrcallback. Instead, accumulate the paths in pure C data structures, and then do the Python transformation oncedl_iterate_phdris done. - Since
dl_iterate_phdris acquiring an external non-Python lock, it should also be wrapped withPy_BEGIN_ALLOW_THREADS.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Modules/_ctypes/callproc.c のリンク先の dllist() 実装から始め、続いて dl_iterate_phdr の契約と、コメントで参照されている再現プログラムを読んでください。ライブラリの反復処理がアクティブな間に callback が Python を呼び出さないことを確認し、報告されている GIL/DL-LOCK デッドロックのシナリオがもはや発生し得ないことを検証してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- c, linux, python
- 領域
- backend, operating-systems
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 活発
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 48/100