diff options
author | Bar Harel <bharel@barharel.com> | 2024-09-04 15:21:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-04 15:21:30 (GMT) |
commit | a4562fedadb73fe1e978dece65c3bcefb4606678 (patch) | |
tree | af2195c8aa1375ae19fabc9ec34c1855b1fe79d3 /Lib | |
parent | c530ce1e9d336b81c053a5985444b4553fdd7050 (diff) | |
download | cpython-a4562fedadb73fe1e978dece65c3bcefb4606678.zip cpython-a4562fedadb73fe1e978dece65c3bcefb4606678.tar.gz cpython-a4562fedadb73fe1e978dece65c3bcefb4606678.tar.bz2 |
gh-123321: Fix Parser/myreadline.c to prevent a segfault during a multi-threaded race (#123323)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_readline.py | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py index 91fd7dd..7d07906 100644 --- a/Lib/test/test_readline.py +++ b/Lib/test/test_readline.py @@ -7,11 +7,12 @@ import sys import tempfile import textwrap import unittest -from test.support import verbose +from test.support import requires_gil_enabled, verbose from test.support.import_helper import import_module from test.support.os_helper import unlink, temp_dir, TESTFN from test.support.pty_helper import run_pty from test.support.script_helper import assert_python_ok +from test.support.threading_helper import requires_working_threading # Skip tests if there is no readline module readline = import_module('readline') @@ -349,6 +350,31 @@ readline.write_history_file(history_file) self.assertEqual(len(lines), history_size) self.assertEqual(lines[-1].strip(), b"last input") + @requires_working_threading() + @requires_gil_enabled() + def test_gh123321_threadsafe(self): + """gh-123321: readline should be thread-safe and not crash""" + script = textwrap.dedent(r""" + import threading + from test.support.threading_helper import join_thread + + def func(): + input() + + thread1 = threading.Thread(target=func) + thread2 = threading.Thread(target=func) + thread1.start() + thread2.start() + join_thread(thread1) + join_thread(thread2) + print("done") + """) + + output = run_pty(script, input=b"input1\rinput2\r") + + self.assertIn(b"done", output) + + def test_write_read_limited_history(self): previous_length = readline.get_history_length() self.addCleanup(readline.set_history_length, previous_length) |