GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [mypyc] Fix free-threading race condition in argument parsing by JukkaL · Pull Request #21613 · python/mypy

[mypyc] Fix free-threading race condition in argument parsing #21613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
JukkaL merged 2 commits into master from mypy-parallel-crash
Jun 15, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

[mypyc] Fix free-threading race condition in argument parsing #21613

Changes from 1 commit
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
cec9990
[mypyc] Fix free-threading race condition in argument parsing
JukkaL Jun 15, 2026
42c21f4
Add regression test
JukkaL Jun 15, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
[mypyc] Fix free-threading race condition in argument parsing
Global argument parser state initialization was missing synchronization.

Don't use a mutex on the hot path as an optimization.

Fixes #21578.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@JukkaL
JukkaL committed Jun 15, 2026
commit cec999057865cd8e0e4fde439931884fb5f2bbd1

There are no files selected for viewing

72 changes: 64 additions & 8 deletions 72 mypyc/lib-rt/getargsfast.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,22 @@
#include <Python.h>
#include "CPy.h"

#define PARSER_INITED(parser) ((parser)->kwtuple != NULL)
// The kwtuple field doubles as the "parser has been initialized" flag: it is
// written last (after all other parser fields) and read first. On free-threaded
// builds the fast paths read it without holding any lock, so the write must be a
// release store and the reads acquire loads. That way a thread that observes a
// non-NULL kwtuple is guaranteed to also see the fully-initialized min/max/etc.
// fields. On GIL builds these are plain accesses with no overhead.
#ifdef Py_GIL_DISABLED
#define PARSER_KWTUPLE(parser) _Py_atomic_load_ptr_acquire(&(parser)->kwtuple)
#define SET_PARSER_KWTUPLE(parser, value) \
_Py_atomic_store_ptr_release(&(parser)->kwtuple, (value))
#else
#define PARSER_KWTUPLE(parser) ((parser)->kwtuple)
#define SET_PARSER_KWTUPLE(parser, value) ((parser)->kwtuple = (value))
#endif

#define PARSER_INITED(parser) (PARSER_KWTUPLE(parser) != NULL)

/* Forward */
static int
Expand Down Expand Up @@ -115,19 +130,21 @@ CPyArg_ParseStackAndKeywordsSimple(PyObject *const *args, Py_ssize_t nargs, PyOb
/* List of static parsers. */
static struct CPyArg_Parser *static_arg_parsers = NULL;

#ifdef Py_GIL_DISABLED
// Serializes one-time initialization of parsers and insertion into the
// static_arg_parsers list. Only contended the first time a given compiled
// function is called; once a parser is initialized the fast paths never lock.
static PyMutex static_arg_parsers_mutex;
#endif

static int
parser_init(CPyArg_Parser *parser)
parser_init_locked(CPyArg_Parser *parser)
{
const char * const *keywords;
const char *format;
int i, len, min, max, nkw;
PyObject *kwtuple;

assert(parser->keywords != NULL);
if (PARSER_INITED(parser)) {
return 1;
}

keywords = parser->keywords;
/* scan keywords and count the number of positional-only parameters */
for (i = 0; keywords[i] && !*keywords[i]; i++) {
Expand Down Expand Up @@ -244,14 +261,53 @@ parser_init(CPyArg_Parser *parser)
PyUnicode_InternInPlace(&str);
PyTuple_SET_ITEM(kwtuple, i, str);
}
parser->kwtuple = kwtuple;

assert(parser->next == NULL);
parser->next = static_arg_parsers;
static_arg_parsers = parser;

// Publish the parser last: storing kwtuple marks it as initialized, so all
// other fields (and the list insertion above) must already be in place. On
// free-threaded builds this is a release store paired with the acquire loads
// in PARSER_INITED/PARSER_KWTUPLE.
SET_PARSER_KWTUPLE(parser, kwtuple);
return 1;
}

// Cold path of parser_init: perform the one-time initialization. On
// free-threaded builds this is serialized so that only one thread builds the
// parser and inserts it into the static_arg_parsers list.
static CPy_NOINLINE int
parser_init_slow(CPyArg_Parser *parser)
{
#ifdef Py_GIL_DISABLED
PyMutex_Lock(&static_arg_parsers_mutex);
// Re-check now that we hold the lock: another thread may have initialized
// the parser while we were waiting.
if (PARSER_INITED(parser)) {
PyMutex_Unlock(&static_arg_parsers_mutex);
return 1;
}
int retval = parser_init_locked(parser);
PyMutex_Unlock(&static_arg_parsers_mutex);
return retval;
#else
return parser_init_locked(parser);
#endif
}

// Hot path: a parser is almost always already initialized, so keep the common
// case inline and branch out to parser_init_slow only on first use.
static inline int
parser_init(CPyArg_Parser *parser)
{
assert(parser->keywords != NULL);
if (likely(PARSER_INITED(parser))) {
return 1;
}
return parser_init_slow(parser);
}

static PyObject*
find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
{
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.