Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
forked frompython/cpython

Commit46006a1

Browse files
stephen-hansenZeroIntensitygpshead
authored
pythongh-127586: properly restore blocked signals in resource_tracker.py (pythonGH-127587)
* Correct pthread_sigmask in resource_tracker to restore old signalsUsing SIG_UNBLOCK to remove blocked "ignored signals" may accidentallycause side effects if the calling parent already had said signalsblocked to begin with and did not intend to unblock them whencreating a pool. Use SIG_SETMASK instead with the previous mask ofblocked signals to restore the original blocked set.* Adding resource_tracker blocked signals testCo-authored-by: Peter Bierma <zintensitydev@gmail.com>Co-authored-by: Gregory P. Smith <greg@krypto.org>
1 parent7b8bd3b commit46006a1

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

‎Lib/multiprocessing/resource_tracker.py‎

Lines changed: 4 additions & 3 deletions
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+
ifprev_sigmaskisnotNone:
165+
signal.pthread_sigmask(signal.SIG_SETMASK,prev_sigmask)
165166
except:
166167
os.close(w)
167168
raise

‎Lib/test/_test_multiprocessing.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6044,6 +6044,21 @@ def test_resource_tracker_exit_code(self):
60446044
self._test_resource_tracker_leak_resources(
60456045
cleanup=cleanup,
60466046
)
6047+
@unittest.skipUnless(hasattr(signal,"pthread_sigmask"),"pthread_sigmask is not available")
6048+
deftest_resource_tracker_blocked_signals(self):
6049+
#
6050+
# gh-127586: Check that resource_tracker does not override blocked signals of caller.
6051+
#
6052+
frommultiprocessing.resource_trackerimportResourceTracker
6053+
signals= {signal.SIGTERM,signal.SIGINT,signal.SIGUSR1}
6054+
6055+
forsiginsignals:
6056+
signal.pthread_sigmask(signal.SIG_SETMASK, {sig})
6057+
self.assertEqual(signal.pthread_sigmask(signal.SIG_BLOCK,set()), {sig})
6058+
tracker=ResourceTracker()
6059+
tracker.ensure_running()
6060+
self.assertEqual(signal.pthread_sigmask(signal.SIG_BLOCK,set()), {sig})
6061+
tracker._stop()
60476062

60486063
classTestSimpleQueue(unittest.TestCase):
60496064

Lines changed: 3 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp