Skip to content

Commit f586fd9

Browse files
authored
gh-144774: Add critical section in BaseException.__setstate__ (#150578)
1 parent 25a5d87 commit f586fd9

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import unittest
2+
import copy
3+
4+
from test.support import threading_helper
5+
6+
threading_helper.requires_working_threading(module=True)
7+
class ExceptionTests(unittest.TestCase):
8+
def test_setstate_data_race(self):
9+
E = Exception()
10+
11+
def func():
12+
for i in range(100):
13+
setattr(E, 'x', i)
14+
copy.copy(E)
15+
16+
threading_helper.run_concurrently(func, nthreads=4)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix data race in :class:`BaseException` when an exception is copied while being mutated.

Objects/clinic/exceptions.c.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/exceptions.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,15 @@ BaseException___reduce___impl(PyBaseExceptionObject *self)
225225
*/
226226

227227
/*[clinic input]
228-
@critical_section
228+
@critical_section state
229229
BaseException.__setstate__
230230
state: object
231231
/
232232
[clinic start generated code]*/
233233

234234
static PyObject *
235235
BaseException___setstate___impl(PyBaseExceptionObject *self, PyObject *state)
236-
/*[clinic end generated code: output=f3834889950453ab input=5524b61cfe9b9856]*/
236+
/*[clinic end generated code: output=f3834889950453ab input=f9b1aea70382cdb6]*/
237237
{
238238
PyObject *d_key, *d_value;
239239
Py_ssize_t i = 0;

0 commit comments

Comments
 (0)