gh-129701: Fix a data race in intern_common in the free threaded build (GH-130089)

* gh-129701: Fix a data race in `intern_common` in the free threaded build

* Use a mutex to avoid potentially returning a non-immortalized string,
  because immortalization happens after the insertion into the interned
  dict.

* Use `Py_DECREF()` calls instead of `Py_SET_REFCNT(s, Py_REFCNT(s) - 2)`
  for thread-safety. This code path isn't performance sensistive, so
  just use `Py_DECREF()` unconditionally for simplicity.
This commit is contained in:
Sam Gross 2025-02-17 08:15:40 -05:00 committed by GitHub
parent fc8c99a8ce
commit b9d2ee687c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 6 deletions

View file

@ -64,6 +64,9 @@ struct _Py_static_objects {
(interp)->cached_objects.NAME
struct _Py_interp_cached_objects {
#ifdef Py_GIL_DISABLED
PyMutex interned_mutex;
#endif
PyObject *interned_strings;
/* object.__reduce__ */