Skip to content

Commit 8c50080

Browse files
[3.15] gh-153292: Fix data race in threading.RLock.__repr__ in FT builds (GH-153299) (#153314)
gh-153292: Fix data race in `threading.RLock.__repr__` in FT builds (GH-153299) (cherry picked from commit 1051384) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent e597358 commit 8c50080

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import unittest
2+
from test.support import threading_helper
3+
4+
threading_helper.requires_working_threading(module=True)
5+
6+
7+
class TestRlock(unittest.TestCase):
8+
def test_repr_race(self):
9+
# gh-153292
10+
import _thread
11+
r = _thread.RLock()
12+
13+
def repr_thread():
14+
for _ in range(2000):
15+
repr(r)
16+
17+
def mutate_thread():
18+
for _ in range(2000):
19+
r.acquire()
20+
r.release()
21+
22+
threading_helper.run_concurrently([repr_thread, mutate_thread])
23+
24+
25+
if __name__ == "__main__":
26+
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix data race in repr of :class:`threading.RLock` in free-threading build.

Modules/_threadmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ static PyObject *
12881288
rlock_repr(PyObject *op)
12891289
{
12901290
rlockobject *self = rlockobject_CAST(op);
1291-
PyThread_ident_t owner = self->lock.thread;
1291+
PyThread_ident_t owner = FT_ATOMIC_LOAD_ULLONG_RELAXED(self->lock.thread);
12921292
int locked = rlock_locked_impl(self);
12931293
size_t count;
12941294
if (locked) {

0 commit comments

Comments
 (0)