bpo-31731: Fix test_io.check_interrupted_write() (GH-11225)

Fix a race condition in check_interrupted_write() of test_io:
create directly the thread with SIGALRM signal blocked,
rather than blocking the signal later from the thread. Previously, it
was possible that the thread gets the signal before the signal is
blocked.
(cherry picked from commit 05c9d31eb6)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
This commit is contained in:
Miss Islington (bot) 2018-12-18 15:10:47 -08:00 committed by GitHub
parent 12f3979b38
commit 729fc5d2ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View File

@ -4121,10 +4121,9 @@ class SignalsTest(unittest.TestCase):
in the latter."""
read_results = []
def _read():
if hasattr(signal, 'pthread_sigmask'):
signal.pthread_sigmask(signal.SIG_BLOCK, [signal.SIGALRM])
s = os.read(r, 1)
read_results.append(s)
t = threading.Thread(target=_read)
t.daemon = True
r, w = os.pipe()
@ -4132,7 +4131,14 @@ class SignalsTest(unittest.TestCase):
large_data = item * (support.PIPE_MAX_SIZE // len(item) + 1)
try:
wio = self.io.open(w, **fdopen_kwargs)
t.start()
if hasattr(signal, 'pthread_sigmask'):
# create the thread with SIGALRM signal blocked
signal.pthread_sigmask(signal.SIG_BLOCK, [signal.SIGALRM])
t.start()
signal.pthread_sigmask(signal.SIG_UNBLOCK, [signal.SIGALRM])
else:
t.start()
# Fill the pipe enough that the write will be blocking.
# It will be interrupted by the timer armed above. Since the
# other thread has read one byte, the low-level write will

View File

@ -0,0 +1,4 @@
Fix a race condition in ``check_interrupted_write()`` of test_io: create
directly the thread with SIGALRM signal blocked, rather than blocking the
signal later from the thread. Previously, it was possible that the thread gets
the signal before the signal is blocked.