Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

sysconfig.get_config_var is not thread-safe #92452

Closed
Labels
type-bugAn unexpected behavior, bug, or error
@gareth-rees

Description

@gareth-rees

Reproducer

This program starts many threads racing to callsysconfig.get_config_var:

import sysconfigfrom multiprocessing.pool import ThreadPooldef thread(i):    assert sysconfig.get_config_var("srcdir") is not Nonewith ThreadPool() as pool:    pool.map(thread, range(100))

When run with Python 3.11.0, this asserts as follows:

Traceback (most recent call last):  File "/home/grees/core/junk.py", line 8, in <module>    pool.map(thread, range(100))    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/home/grees/github.com/cpython/Lib/multiprocessing/pool.py", line 364, in map    return self._map_async(func, iterable, mapstar, chunksize).get()           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/home/grees/github.com/cpython/Lib/multiprocessing/pool.py", line 771, in get    raise self._value    ^^^^^^^^^^^^^^^^^  File "/home/grees/github.com/cpython/Lib/multiprocessing/pool.py", line 125, in worker    result = (True, func(*args, **kwds))                    ^^^^^^^^^^^^^^^^^^^  File "/home/grees/github.com/cpython/Lib/multiprocessing/pool.py", line 48, in mapstar    return list(map(*args))           ^^^^^^^^^^^^^^^^  File "/home/grees/core/junk.py", line 5, in thread    assert sysconfig.get_config_var("srcdir") is not None    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^AssertionError

Analysis

In thesysconfig module,get_config_var callsget_config_vars which initializes the global variable_CONFIG_VARS in a thread-unsafe manner.

Use case

The reproducer above is simplified from a multi-threaded build system. Each build thread needs to get some information fromsysconfig, so that the threads are racing to be the first to callget_config_vars.

Workaround

Adding an initial call toget_config_vars before starting any thread ensures that_CONFIG_VARS is reliably initialized.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp