Data race in the `codec.errors` setter (`codecctx_errors_set`) on a shared stateful codec
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
Bug report
Bug description:
Bug description:
In the free-threaded build, the codecctx_errors_set decrefs the old handler(ERROR_DECREF(self->errors)) and stores the new one (self->errors = cb) without any synchronization
For any handler name other than the strict/ignore/replace sentinels (e.g. backslashreplace), self->errors holds a real refcounted PyUnicode.
The decref-then-assign is not atomic, so concurrent codec.errors = drops references incorrectly and can free a handler while it is still referenced.
Reproducer:
import codecs
import random
from threading import Thread
shared = codecs.getincrementalencoder('shift_jis')()
NAMES = ['backslashreplace', 'xmlcharrefreplace', 'namereplace']
def thread1():
for _ in range(20000):
try:
shared.errors = random.choice(NAMES)
except Exception:
pass
if __name__ == "__main__":
threads = [Thread(target=thread1) for _ in range(8)]
for t in threads: t.start()
for t in threads: t.join()
TSAN Report:
==================
WARNING: ThreadSanitizer: data race (pid=3576762)
Read of size 8 at 0x7fffb65a1cc0 by thread T2:
#0 codecctx_errors_set /cpython/./Modules/cjkcodecs/multibytecodec.c:194:5
#1 getset_set /cpython/Objects/descrobject.c:250:16
#2 _PyObject_GenericSetAttrWithDict /cpython/Objects/object.c:2049:19
#3 PyObject_GenericSetAttr /cpython/Objects/object.c:2120:12
#4 PyObject_SetAttr /cpython/Objects/object.c:1533:15
#5 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:12146:27
...
Previous write of size 8 at 0x7fffb65a1cc0 by thread T1:
#0 codecctx_errors_set /cpython/./Modules/cjkcodecs/multibytecodec.c:195:18
#1 getset_set /cpython/Objects/descrobject.c:250:16
#2 _PyObject_GenericSetAttrWithDict /cpython/Objects/object.c:2049:19
#3 PyObject_GenericSetAttr /cpython/Objects/object.c:2120:12
#4 PyObject_SetAttr /cpython/Objects/object.c:1533:15
#5 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:12146:27
...
SUMMARY: ThreadSanitizer: data race /cpython/./Modules/cjkcodecs/multibytecodec.c:194:5 in codecctx_errors_set
==================
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-153000
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Modules/cjkcodecs/multibytecodec.c の codecctx_errors_set から始め、次に free-threaded build 上で ThreadSanitizer を使って、提供されたスレッド付き reproducer を実行します。リンクされた PR gh-153000 と動作を比較します。完了の条件は、codec.errors の並行代入によって報告された race や handler の不正なライフタイム動作が発生しなくなることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- c, python
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 30/100