Skip to content

Commit 2d0752e

Browse files
[3.14] gh-153292: Fix data race in threading.RLock.__repr__ in FT builds (GH-153299) (#153315)
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 50300ac commit 2d0752e

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
@@ -1215,7 +1215,7 @@ static PyObject *
12151215
rlock_repr(PyObject *op)
12161216
{
12171217
rlockobject *self = rlockobject_CAST(op);
1218-
PyThread_ident_t owner = self->lock.thread;
1218+
PyThread_ident_t owner = FT_ATOMIC_LOAD_ULLONG_RELAXED(self->lock.thread);
12191219
int locked = rlock_locked_impl(self);
12201220
size_t count;
12211221
if (locked) {

0 commit comments

Comments
 (0)