[Python-checkins] gh-105699: Use a Thread-Local Variable for PKGCONTEXT (gh-105740)
ericsnowcurrently
webhook-mailer at python.org
Tue Jun 13 20:58:31 EDT 2023
https://github.com/python/cpython/commit/b87d2882754a7c273e2695c33384383eba380d7d
commit: b87d2882754a7c273e2695c33384383eba380d7d
branch: main
author: Eric Snow <ericsnowcurrently at gmail.com>
committer: ericsnowcurrently <ericsnowcurrently at gmail.com>
date: 2023-06-13T18:58:23-06:00
summary:
gh-105699: Use a Thread-Local Variable for PKGCONTEXT (gh-105740)
This fixes a race during import. The existing _PyRuntimeState.imports.pkgcontext is shared between interpreters, and occasionally this would cause a crash when multiple interpreters were importing extensions modules at the same time. To solve this we add a thread-local variable for the value. We also leave the existing state (and infrequent race) in place for platforms that do not support thread-local variables.
files:
M Python/import.c
M Tools/c-analyzer/c_parser/parser/_regexes.py
M Tools/c-analyzer/c_parser/preprocessor/gcc.py
M Tools/c-analyzer/cpython/ignored.tsv
diff --git a/Python/import.c b/Python/import.c
index 71cce8e4f623d..779af55347a61 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -703,10 +703,19 @@ _PyImport_ClearModulesByIndex(PyInterpreterState *interp)
_PyRuntime.imports.pkgcontext, and PyModule_Create*() will
substitute this (if the name actually matches).
*/
+
+#ifdef HAVE_THREAD_LOCAL
+_Py_thread_local const char *pkgcontext = NULL;
+# undef PKGCONTEXT
+# define PKGCONTEXT pkgcontext
+#endif
+
const char *
_PyImport_ResolveNameWithPackageContext(const char *name)
{
+#ifndef HAVE_THREAD_LOCAL
PyThread_acquire_lock(EXTENSIONS.mutex, WAIT_LOCK);
+#endif
if (PKGCONTEXT != NULL) {
const char *p = strrchr(PKGCONTEXT, '.');
if (p != NULL && strcmp(name, p+1) == 0) {
@@ -714,17 +723,23 @@ _PyImport_ResolveNameWithPackageContext(const char *name)
PKGCONTEXT = NULL;
}
}
+#ifndef HAVE_THREAD_LOCAL
PyThread_release_lock(EXTENSIONS.mutex);
+#endif
return name;
}
const char *
_PyImport_SwapPackageContext(const char *newcontext)
{
+#ifndef HAVE_THREAD_LOCAL
PyThread_acquire_lock(EXTENSIONS.mutex, WAIT_LOCK);
+#endif
const char *oldcontext = PKGCONTEXT;
PKGCONTEXT = newcontext;
+#ifndef HAVE_THREAD_LOCAL
PyThread_release_lock(EXTENSIONS.mutex);
+#endif
return oldcontext;
}
diff --git a/Tools/c-analyzer/c_parser/parser/_regexes.py b/Tools/c-analyzer/c_parser/parser/_regexes.py
index b7f22b186f496..5695daff67d6b 100644
--- a/Tools/c-analyzer/c_parser/parser/_regexes.py
+++ b/Tools/c-analyzer/c_parser/parser/_regexes.py
@@ -58,6 +58,7 @@ def _ind(text, level=1, edges='both'):
extern |
register |
static |
+ _Thread_local |
typedef |
const |
@@ -137,7 +138,7 @@ def _ind(text, level=1, edges='both'):
#######################################
# variable declarations
-_STORAGE = 'auto register static extern'.split()
+_STORAGE = 'auto register static extern _Thread_local'.split()
STORAGE_CLASS = rf'(?: \b (?: {" | ".join(_STORAGE)} ) \b )'
TYPE_QUALIFIER = r'(?: \b (?: const | volatile ) \b )'
PTR_QUALIFIER = rf'(?: [*] (?: \s* {TYPE_QUALIFIER} )? )'
diff --git a/Tools/c-analyzer/c_parser/preprocessor/gcc.py b/Tools/c-analyzer/c_parser/preprocessor/gcc.py
index c680f351f2241..147615707a3cb 100644
--- a/Tools/c-analyzer/c_parser/preprocessor/gcc.py
+++ b/Tools/c-analyzer/c_parser/preprocessor/gcc.py
@@ -219,6 +219,7 @@ def _strip_directives(line, partial=0):
line = line[m.end():]
line = re.sub(r'__extension__', '', line)
+ line = re.sub(r'__thread\b', '_Thread_local', line)
while (m := COMPILER_DIRECTIVE_RE.match(line)):
before, _, _, closed = m.groups()
diff --git a/Tools/c-analyzer/cpython/ignored.tsv b/Tools/c-analyzer/cpython/ignored.tsv
index 87d9b39c16113..c58dfe2d0b944 100644
--- a/Tools/c-analyzer/cpython/ignored.tsv
+++ b/Tools/c-analyzer/cpython/ignored.tsv
@@ -168,6 +168,12 @@ Modules/_xxinterpchannelsmodule.c - _globals -
Python/pyfpe.c - PyFPE_counter -
+##-----------------------
+## thread-local variables
+
+Python/import.c - pkgcontext -
+Python/pystate.c - _Py_tss_tstate -
+
##-----------------------
## should be const
# XXX Make them const.
More information about the Python-checkins
mailing list