Skip to content

Commit d0d64ad

Browse files
pierreglaserpitrou
authored andcommitted
bpo-36368: Ignore SIGINT in SharedMemoryManager servers. (GH-12483)
Fix a bug crashing SharedMemoryManager instances in interactive sessions after a Ctrl-C (KeyboardInterrupt) was sent.
1 parent 86ea581 commit d0d64ad

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Lib/multiprocessing/managers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import sys
1919
import threading
20+
import signal
2021
import array
2122
import queue
2223
import time
@@ -596,6 +597,9 @@ def _run_server(cls, registry, address, authkey, serializer, writer,
596597
'''
597598
Create a server, report its address and run it
598599
'''
600+
# bpo-36368: protect server process from KeyboardInterrupt signals
601+
signal.signal(signal.SIGINT, signal.SIG_IGN)
602+
599603
if initializer is not None:
600604
initializer(*initargs)
601605

Lib/test/_test_multiprocessing.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3734,6 +3734,30 @@ def test_shared_memory_across_processes(self):
37343734

37353735
sms.close()
37363736

3737+
@unittest.skipIf(os.name != "posix", "not feasible in non-posix platforms")
3738+
def test_shared_memory_SharedMemoryServer_ignores_sigint(self):
3739+
# bpo-36368: protect SharedMemoryManager server process from
3740+
# KeyboardInterrupt signals.
3741+
smm = multiprocessing.managers.SharedMemoryManager()
3742+
smm.start()
3743+
3744+
# make sure the manager works properly at the beginning
3745+
sl = smm.ShareableList(range(10))
3746+
3747+
# the manager's server should ignore KeyboardInterrupt signals, and
3748+
# maintain its connection with the current process, and success when
3749+
# asked to deliver memory segments.
3750+
os.kill(smm._process.pid, signal.SIGINT)
3751+
3752+
sl2 = smm.ShareableList(range(10))
3753+
3754+
# test that the custom signal handler registered in the Manager does
3755+
# not affect signal handling in the parent process.
3756+
with self.assertRaises(KeyboardInterrupt):
3757+
os.kill(os.getpid(), signal.SIGINT)
3758+
3759+
smm.shutdown()
3760+
37373761
def test_shared_memory_SharedMemoryManager_basics(self):
37383762
smm1 = multiprocessing.managers.SharedMemoryManager()
37393763
with self.assertRaises(ValueError):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug crashing SharedMemoryManager instances in interactive sessions after
2+
a ctrl-c (KeyboardInterrupt) was sent

0 commit comments

Comments
 (0)