Skip to content

Commit e9b3c92

Browse files
brijkapadianascheme
authored andcommitted
[3.14] gh-144774: Add critical section in BaseException.__setstate__ (GH-150578)
(cherry picked from commit f586fd9) Co-authored-by: Brij Kapadia <97006829+brijkapadia@users.noreply.github.com>
1 parent c628cd7 commit e9b3c92

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
@@ -233,15 +233,15 @@ BaseException___reduce___impl(PyBaseExceptionObject *self)
233233
*/
234234

235235
/*[clinic input]
236-
@critical_section
236+
@critical_section state
237237
BaseException.__setstate__
238238
state: object
239239
/
240240
[clinic start generated code]*/
241241

242242
static PyObject *
243243
BaseException___setstate___impl(PyBaseExceptionObject *self, PyObject *state)
244-
/*[clinic end generated code: output=f3834889950453ab input=5524b61cfe9b9856]*/
244+
/*[clinic end generated code: output=f3834889950453ab input=f9b1aea70382cdb6]*/
245245
{
246246
PyObject *d_key, *d_value;
247247
Py_ssize_t i = 0;

0 commit comments

Comments
 (0)