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

Appearance settings

Commit 341b86e

Browse filesBrowse files
[3.13] gh-132713: Fix typing.Union[index] race condition (GH-132802) (#132839)
gh-132713: Fix typing.Union[index] race condition (GH-132802) Add union_init_parameters() helper function. Use a critical section to initialize the 'parameters' member. (cherry picked from commit dc3e963) Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 3de0f55 commit 341b86e
Copy full SHA for 341b86e

File tree

Expand file treeCollapse file tree

1 file changed

+19
-10
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+19
-10
lines changed

‎Objects/unionobject.c

Copy file name to clipboardExpand all lines: Objects/unionobject.c
+19-10Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,29 @@ static PyMemberDef union_members[] = {
270270
{0}
271271
};
272272

273-
static PyObject *
274-
union_getitem(PyObject *self, PyObject *item)
273+
// Populate __parameters__ if needed.
274+
static int
275+
union_init_parameters(unionobject *alias)
275276
{
276-
unionobject *alias = (unionobject *)self;
277-
// Populate __parameters__ if needed.
277+
int result = 0;
278+
Py_BEGIN_CRITICAL_SECTION(alias);
278279
if (alias->parameters == NULL) {
279280
alias->parameters = _Py_make_parameters(alias->args);
280281
if (alias->parameters == NULL) {
281-
return NULL;
282+
result = -1;
282283
}
283284
}
285+
Py_END_CRITICAL_SECTION();
286+
return result;
287+
}
288+
289+
static PyObject *
290+
union_getitem(PyObject *self, PyObject *item)
291+
{
292+
unionobject *alias = (unionobject *)self;
293+
if (union_init_parameters(alias) < 0) {
294+
return NULL;
295+
}
284296

285297
PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item);
286298
if (newargs == NULL) {
@@ -314,11 +326,8 @@ static PyObject *
314326
union_parameters(PyObject *self, void *Py_UNUSED(unused))
315327
{
316328
unionobject *alias = (unionobject *)self;
317-
if (alias->parameters == NULL) {
318-
alias->parameters = _Py_make_parameters(alias->args);
319-
if (alias->parameters == NULL) {
320-
return NULL;
321-
}
329+
if (union_init_parameters(alias) < 0) {
330+
return NULL;
322331
}
323332
return Py_NewRef(alias->parameters);
324333
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.