python / python/cpython

Data race on `kqueue.kqfd` between `close()` and the `closed` getter with free-threading build

オープン
#151,364 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

topic-free-threading type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

select.kqueue accesses its internal kqfd file-descriptor field with no synchronization. So kqueue.close() can write the kqfd,

https://github.com/python/cpython/blob/b18168cb32d545ed976b760983478cbd5dde5bdf/Modules/selectmodule.c#L2160-L2165

while the closed getter reads it.

https://github.com/python/cpython/blob/b18168cb32d545ed976b760983478cbd5dde5bdf/Modules/selectmodule.c#L2276-L2279

Reproducer:

import select
from threading import Thread

slot = [select.kqueue()]

def reader():
    for _ in range(200000):
        try:
            _ = slot[0].closed
        except Exception:
            pass

def churner():
    for _ in range(200000):
        kq = select.kqueue()
        slot[0] = kq
        kq.close()

threads  = [Thread(target=reader)  for _ in range(4)]
threads += [Thread(target=churner) for _ in range(4)]
for t in threads: t.start()
for t in threads: t.join()

TSAN Report:

==================
WARNING: ThreadSanitizer: data race (pid=28784)
  Write of size 4 at 0x00011c040140 by thread T5:
    #0 kqueue_queue_internal_close selectmodule.c:2165
    #1 select_kqueue_close selectmodule.c.h:1189
    #2 method_vectorcall_NOARGS descrobject.c:448
    #3 PyObject_Vectorcall call.c:327
    #4 _Py_VectorCallInstrumentation_StackRefSteal ceval.c:766
    #5 _PyEval_EvalFrameDefault generated_cases.c.h:1846
...

  Previous read of size 4 at 0x00011c040140 by thread T4:
    #0 kqueue_queue_get_closed selectmodule.c:2279
    #1 getset_get descrobject.c:194
    #2 _PyObject_GenericGetAttrWithDict object.c
    #3 PyObject_GenericGetAttr object.c:2012
    #4 _PyObject_GetAttrStackRef object.c
    #5 _PyEval_EvalFrameDefault generated_cases.c.h:8312

SUMMARY: ThreadSanitizer: data race selectmodule.c:2165 in kqueue_queue_internal_close
==================
CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs
  • gh-151376

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Modules/selectmodule.c の 2165 行付近にある kqueue_queue_internal_close と、2279 行付近にある kqueue_queue_get_closed から始め、その後 reproducer と TSAN レポートを確認してください。すでに進行中の作業について、リンク先の PR gh-151376 を確認してください。concurrent な close() と closed へのアクセスによって、報告された race または descriptor hazard が発生しなくなれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, python
領域
operating-systems
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
20/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。