Skip to content

Commit 863d54c

Browse files
authored
gh-126895: Fix readline module in free-threaded build (#131208)
The underlying readline library is not thread-safe so this adds `@critical_section` to functions that use it.
1 parent 9a634c5 commit 863d54c

File tree

4 files changed

+208
-46
lines changed

4 files changed

+208
-46
lines changed

Diff for: Lib/test/test_readline.py

+24
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import sys
77
import tempfile
88
import textwrap
9+
import threading
910
import unittest
11+
from test import support
12+
from test.support import threading_helper
1013
from test.support import verbose
1114
from test.support.import_helper import import_module
1215
from test.support.os_helper import unlink, temp_dir, TESTFN
@@ -403,5 +406,26 @@ def test_write_read_limited_history(self):
403406
# See TestHistoryManipulation for the full test.
404407

405408

409+
@unittest.skipUnless(support.Py_GIL_DISABLED, 'these tests can only possibly fail with GIL disabled')
410+
class FreeThreadingTest(unittest.TestCase):
411+
@threading_helper.reap_threads
412+
@threading_helper.requires_working_threading()
413+
def test_free_threading(self):
414+
def completer_delims(b):
415+
b.wait()
416+
for _ in range(100):
417+
readline.get_completer_delims()
418+
readline.set_completer_delims(' \t\n`@#%^&*()=+[{]}\\|;:\'",<>?')
419+
readline.set_completer_delims(' \t\n`@#%^&*()=+[{]}\\|;:\'",<>?')
420+
readline.get_completer_delims()
421+
422+
count = 40
423+
barrier = threading.Barrier(count)
424+
threads = [threading.Thread(target=completer_delims, args=(barrier,)) for _ in range(count)]
425+
426+
with threading_helper.start_threads(threads):
427+
pass
428+
429+
406430
if __name__ == "__main__":
407431
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :mod:`readline` in :term:`free-threaded <free threading>` build.

Diff for: Modules/clinic/readline.c.h

+119-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)