Skip to content

Commit 16fde76

Browse files
miss-islingtonstephen-hansenZeroIntensitygpshead
authored
[3.13] gh-127586: multiprocessing.Pool does not properly restore blocked signals (try 2) (GH-128011) (#128298)
gh-127586: multiprocessing.Pool does not properly restore blocked signals (try 2) (GH-128011) Correct pthread_sigmask in resource_tracker to restore old signals Using SIG_UNBLOCK to remove blocked "ignored signals" may accidentally cause side effects if the calling parent already had said signals blocked to begin with and did not intend to unblock them when creating a pool. Use SIG_SETMASK instead with the previous mask of blocked signals to restore the original blocked set. (cherry picked from commit aeb9b65) Co-authored-by: Stephen Hansen <[email protected]> Co-authored-by: Peter Bierma <[email protected]> Co-authored-by: Gregory P. Smith <[email protected]>
1 parent aea2e03 commit 16fde76

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

Lib/multiprocessing/resource_tracker.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,14 @@ def ensure_running(self):
155155
# that can make the child die before it registers signal handlers
156156
# for SIGINT and SIGTERM. The mask is unregistered after spawning
157157
# the child.
158+
prev_sigmask = None
158159
try:
159160
if _HAVE_SIGMASK:
160-
signal.pthread_sigmask(signal.SIG_BLOCK, _IGNORED_SIGNALS)
161+
prev_sigmask = signal.pthread_sigmask(signal.SIG_BLOCK, _IGNORED_SIGNALS)
161162
pid = util.spawnv_passfds(exe, args, fds_to_pass)
162163
finally:
163-
if _HAVE_SIGMASK:
164-
signal.pthread_sigmask(signal.SIG_UNBLOCK, _IGNORED_SIGNALS)
164+
if prev_sigmask is not None:
165+
signal.pthread_sigmask(signal.SIG_SETMASK, prev_sigmask)
165166
except:
166167
os.close(w)
167168
raise

Lib/test/_test_multiprocessing.py

+21
Original file line numberDiff line numberDiff line change
@@ -5950,6 +5950,27 @@ def test_resource_tracker_exit_code(self):
59505950
cleanup=cleanup,
59515951
)
59525952

5953+
@unittest.skipUnless(hasattr(signal, "pthread_sigmask"), "pthread_sigmask is not available")
5954+
def test_resource_tracker_blocked_signals(self):
5955+
#
5956+
# gh-127586: Check that resource_tracker does not override blocked signals of caller.
5957+
#
5958+
from multiprocessing.resource_tracker import ResourceTracker
5959+
orig_sigmask = signal.pthread_sigmask(signal.SIG_BLOCK, set())
5960+
signals = {signal.SIGTERM, signal.SIGINT, signal.SIGUSR1}
5961+
5962+
try:
5963+
for sig in signals:
5964+
signal.pthread_sigmask(signal.SIG_SETMASK, {sig})
5965+
self.assertEqual(signal.pthread_sigmask(signal.SIG_BLOCK, set()), {sig})
5966+
tracker = ResourceTracker()
5967+
tracker.ensure_running()
5968+
self.assertEqual(signal.pthread_sigmask(signal.SIG_BLOCK, set()), {sig})
5969+
tracker._stop()
5970+
finally:
5971+
# restore sigmask to what it was before executing test
5972+
signal.pthread_sigmask(signal.SIG_SETMASK, orig_sigmask)
5973+
59535974
class TestSimpleQueue(unittest.TestCase):
59545975

59555976
@classmethod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:class:`multiprocessing.pool.Pool` now properly restores blocked signal handlers
2+
of the parent thread when creating processes via either *spawn* or
3+
*forkserver*.

0 commit comments

Comments
 (0)