Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,21 @@ def test_exec_set_nomemory_hang(self):
self.assertGreater(len(output), 0) # At minimum, should not hang
self.assertIn(b"MemoryError", output)

def test_setstate_thread_safety(self):
import threading
import random
exc = Exception()
def worker():
for _ in range(100):
setattr(exc, "x", random.randint(0, 1000))
copy.copy(exc)

threads = [threading.Thread(target=worker) for _ in range(4)]
for t in threads:
t.start()
for t in threads:
t.join()
self.assertTrue(True)

class NameErrorTests(unittest.TestCase):
def test_name_error_has_name(self):
Expand Down
5 changes: 5 additions & 0 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,21 @@
PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
return NULL;
}
PyCriticalSection cs;

Check failure on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'cs' uses undefined struct 'PyCriticalSection' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

unused variable ‘cs’ [-Wunused-variable]

Check failure on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

storage size of ‘cs’ isn’t known

Check warning on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

unused variable ‘cs’ [-Wunused-variable]

Check failure on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

storage size of ‘cs’ isn’t known

Check failure on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'cs' uses undefined struct 'PyCriticalSection' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

unused variable ‘cs’ [-Wunused-variable]

Check failure on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

storage size of ‘cs’ isn’t known

Check warning on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

unused variable ‘cs’ [-Wunused-variable]

Check failure on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

storage size of ‘cs’ isn’t known

Check warning on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

unused variable ‘cs’ [-Wunused-variable]

Check failure on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

storage size of ‘cs’ isn’t known

Check warning on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

unused variable ‘cs’ [-Wunused-variable]

Check failure on line 245 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

storage size of ‘cs’ isn’t known
PyThreadState *tstate = _PyThreadState_GET();
_PyCriticalSection_Begin(tstate, &cs, state);

Check warning on line 247 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'_PyCriticalSection_Begin' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 247 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

implicit declaration of function ‘_PyCriticalSection_Begin’; did you mean ‘PyCriticalSection_Begin’? [-Werror=implicit-function-declaration]

Check failure on line 247 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

implicit declaration of function ‘_PyCriticalSection_Begin’; did you mean ‘PyCriticalSection_Begin’? [-Werror=implicit-function-declaration]

Check warning on line 247 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'_PyCriticalSection_Begin' undefined; assuming extern returning int [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 247 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

implicit declaration of function ‘_PyCriticalSection_Begin’; did you mean ‘PyCriticalSection_Begin’? [-Werror=implicit-function-declaration]

Check failure on line 247 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

implicit declaration of function ‘_PyCriticalSection_Begin’; did you mean ‘PyCriticalSection_Begin’? [-Werror=implicit-function-declaration]

Check failure on line 247 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

implicit declaration of function ‘_PyCriticalSection_Begin’; did you mean ‘PyCriticalSection_Begin’? [-Werror=implicit-function-declaration]

Check failure on line 247 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

implicit declaration of function ‘_PyCriticalSection_Begin’; did you mean ‘PyCriticalSection_Begin’? [-Werror=implicit-function-declaration]
while (PyDict_Next(state, &i, &d_key, &d_value)) {
Py_INCREF(d_key);
Py_INCREF(d_value);
int res = PyObject_SetAttr((PyObject *)self, d_key, d_value);
Py_DECREF(d_value);
Py_DECREF(d_key);
if (res < 0) {
_PyCriticalSection_End(tstate, &cs);

Check warning on line 255 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'_PyCriticalSection_End' undefined; assuming extern returning int [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 255 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

implicit declaration of function ‘_PyCriticalSection_End’; did you mean ‘PyCriticalSection_End’? [-Werror=implicit-function-declaration]

Check failure on line 255 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

implicit declaration of function ‘_PyCriticalSection_End’; did you mean ‘PyCriticalSection_End’? [-Werror=implicit-function-declaration]

Check warning on line 255 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'_PyCriticalSection_End' undefined; assuming extern returning int [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 255 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

implicit declaration of function ‘_PyCriticalSection_End’; did you mean ‘PyCriticalSection_End’? [-Werror=implicit-function-declaration]

Check failure on line 255 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

implicit declaration of function ‘_PyCriticalSection_End’; did you mean ‘PyCriticalSection_End’? [-Werror=implicit-function-declaration]

Check failure on line 255 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

implicit declaration of function ‘_PyCriticalSection_End’; did you mean ‘PyCriticalSection_End’? [-Werror=implicit-function-declaration]

Check failure on line 255 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

implicit declaration of function ‘_PyCriticalSection_End’; did you mean ‘PyCriticalSection_End’? [-Werror=implicit-function-declaration]
return NULL;
}
}
_PyCriticalSection_End(tstate, &cs);
}
Py_RETURN_NONE;
}
Expand Down
Loading