Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 05c9d31

Browse files
authored
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.
1 parent b5c8cfa commit 05c9d31

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

Lib/test/test_io.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4149,18 +4149,24 @@ def check_interrupted_write(self, item, bytes, **fdopen_kwargs):
41494149
in the latter."""
41504150
read_results = []
41514151
def _read():
4152-
if hasattr(signal, 'pthread_sigmask'):
4153-
signal.pthread_sigmask(signal.SIG_BLOCK, [signal.SIGALRM])
41544152
s = os.read(r, 1)
41554153
read_results.append(s)
4154+
41564155
t = threading.Thread(target=_read)
41574156
t.daemon = True
41584157
r, w = os.pipe()
41594158
fdopen_kwargs["closefd"] = False
41604159
large_data = item * (support.PIPE_MAX_SIZE // len(item) + 1)
41614160
try:
41624161
wio = self.io.open(w, **fdopen_kwargs)
4163-
t.start()
4162+
if hasattr(signal, 'pthread_sigmask'):
4163+
# create the thread with SIGALRM signal blocked
4164+
signal.pthread_sigmask(signal.SIG_BLOCK, [signal.SIGALRM])
4165+
t.start()
4166+
signal.pthread_sigmask(signal.SIG_UNBLOCK, [signal.SIGALRM])
4167+
else:
4168+
t.start()
4169+
41644170
# Fill the pipe enough that the write will be blocking.
41654171
# It will be interrupted by the timer armed above. Since the
41664172
# other thread has read one byte, the low-level write will
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a race condition in ``check_interrupted_write()`` of test_io: create
2+
directly the thread with SIGALRM signal blocked, rather than blocking the
3+
signal later from the thread. Previously, it was possible that the thread gets
4+
the signal before the signal is blocked.

0 commit comments

Comments
 (0)