Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1954,6 +1954,24 @@ def test_exec_set_nomemory_hang(self):
self.assertGreater(len(output), 0) # At minimum, should not hang
self.assertIn(b"MemoryError", output)

Comment thread
brijkapadia marked this conversation as resolved.
def test_data_race(self):
Comment thread
brijkapadia marked this conversation as resolved.
Outdated
from threading import Thread
import copy

E = Exception()

def func():
for i in range(100):
setattr(E, 'x', i)
copy.copy(E)

threads = [Thread(target=func) for _ in range(4)]
Comment thread
brijkapadia marked this conversation as resolved.
Outdated

for t in threads:
t.start()

for t in threads:
t.join()

class NameErrorTests(unittest.TestCase):
def test_name_error_has_name(self):
Expand Down
9 changes: 8 additions & 1 deletion Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,23 @@ BaseException___setstate___impl(PyBaseExceptionObject *self, PyObject *state)
PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
return NULL;
}
int error = 0;
Py_BEGIN_CRITICAL_SECTION(state);
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) {
return NULL;
error = 1;
break;
}
}
Py_END_CRITICAL_SECTION();
if (error) {
return NULL;
}
}
Py_RETURN_NONE;
}
Expand Down
Loading