[Python-checkins] bpo-31731: Fix test_io.check_interrupted_write() (GH-11225)

Miss Islington (bot) webhook-mailer at python.org
Tue Dec 18 18:10:51 EST 2018


https://github.com/python/cpython/commit/729fc5d2acab9eb672c4804092236af5362a4c66
commit: 729fc5d2acab9eb672c4804092236af5362a4c66
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-12-18T15:10:47-08:00
summary:

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 05c9d31eb62cc45dc3c55a5cdb7cbc713d0421db)

Co-authored-by: Victor Stinner <vstinner at redhat.com>

files:
A Misc/NEWS.d/next/Tests/2018-12-18-23-20-39.bpo-31731.tcv85C.rst
M Lib/test/test_io.py

diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 4c206d287f27..405f2d38b494 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -4121,10 +4121,9 @@ def check_interrupted_write(self, item, bytes, **fdopen_kwargs):
         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 @@ def _read():
         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
diff --git a/Misc/NEWS.d/next/Tests/2018-12-18-23-20-39.bpo-31731.tcv85C.rst b/Misc/NEWS.d/next/Tests/2018-12-18-23-20-39.bpo-31731.tcv85C.rst
new file mode 100644
index 000000000000..530977c6e87e
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2018-12-18-23-20-39.bpo-31731.tcv85C.rst
@@ -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.



More information about the Python-checkins mailing list