From 3cfbc49229dbf5742d39a6e2a28a35db8312f4e4 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Mon, 13 Feb 2023 09:39:14 -0800 Subject: [PATCH] moduleobject: fix data races --- Objects/moduleobject.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 5af5f15653c..25b5482f720 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -42,10 +42,11 @@ PyModuleDef_Init(PyModuleDef* def) { assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY); if (def->m_base.m_index == 0) { - _PyRuntime.imports.last_module_index++; + Py_ssize_t last_module_index = + _Py_atomic_add_ssize(&_PyRuntime.imports.last_module_index, 1) + 1; assert(_PyObject_IS_IMMORTAL((PyObject*) def)); Py_SET_TYPE(def, &PyModuleDef_Type); - def->m_base.m_index = _PyRuntime.imports.last_module_index; + def->m_base.m_index = last_module_index; } return (PyObject*)def; }