FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-144774: Fix data race in BaseException.__setstate__ in free-thread… by priyanshu2282-cyber · Pull Request #144828 · python/cpython · GitHub

/ cpython Public
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
15 changes: 15 additions & 0 deletions Lib/test/test_exceptions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
6 changes: 5 additions & 1 deletion Objects/exceptions.c
Show comments Show annotations View file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "osdefs.h" // SEP
#include "clinic/exceptions.c.h"

#include "pycore_critical_section.h"

/*[clinic input]
class BaseException "PyBaseExceptionObject *" "&PyExc_BaseException"
Expand Down Expand Up @@ -242,16 +242,20 @@
PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
return NULL;
}
PyCriticalSection cs;

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 (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 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)

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 / 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 (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
PyCriticalSection_Begin(&cs, state);

Check warning on line 246 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'function': incompatible types - from 'int *' to 'PyCriticalSection *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 246 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'function': incompatible types - from 'int *' to 'PyCriticalSection *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
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(&cs);

Check warning on line 254 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'function': incompatible types - from 'int *' to 'PyCriticalSection *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 254 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'function': incompatible types - from 'int *' to 'PyCriticalSection *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
return NULL;
}
}
PyCriticalSection_End(&cs);

Check warning on line 258 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'function': incompatible types - from 'int *' to 'PyCriticalSection *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 258 in Objects/exceptions.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'function': incompatible types - from 'int *' to 'PyCriticalSection *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
}
Py_RETURN_NONE;
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL