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

Skip to content

Commit 92ff4e1

Browse files
committed
Issue #14666: stop multiprocessing's resource-sharing thread after the tests are done.
Also, block delivery of signals to that thread. Patch by Richard Oudkerk. This will hopefully fix sporadic freezes on the FreeBSD 9.0 buildbot.
1 parent d0880d5 commit 92ff4e1

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

Lib/multiprocessing/reduction.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import socket
4141
import threading
4242
import struct
43+
import signal
4344

4445
from multiprocessing import current_process
4546
from multiprocessing.util import register_after_fork, debug, sub_debug
@@ -209,6 +210,7 @@ def __init__(self):
209210
self._lock = threading.Lock()
210211
self._listener = None
211212
self._address = None
213+
self._thread = None
212214
register_after_fork(self, ResourceSharer._afterfork)
213215

214216
def register(self, send, close):
@@ -227,6 +229,24 @@ def get_connection(ident):
227229
c.send((key, os.getpid()))
228230
return c
229231

232+
def stop(self, timeout=None):
233+
from .connection import Client
234+
with self._lock:
235+
if self._address is not None:
236+
c = Client(self._address, authkey=current_process().authkey)
237+
c.send(None)
238+
c.close()
239+
self._thread.join(timeout)
240+
if self._thread.is_alive():
241+
sub_warn('ResourceSharer thread did not stop when asked')
242+
self._listener.close()
243+
self._thread = None
244+
self._address = None
245+
self._listener = None
246+
for key, (send, close) in self._cache.items():
247+
close()
248+
self._cache.clear()
249+
230250
def _afterfork(self):
231251
for key, (send, close) in self._cache.items():
232252
close()
@@ -239,6 +259,7 @@ def _afterfork(self):
239259
self._listener.close()
240260
self._listener = None
241261
self._address = None
262+
self._thread = None
242263

243264
def _start(self):
244265
from .connection import Listener
@@ -249,12 +270,18 @@ def _start(self):
249270
t = threading.Thread(target=self._serve)
250271
t.daemon = True
251272
t.start()
273+
self._thread = t
252274

253275
def _serve(self):
276+
if hasattr(signal, 'pthread_sigmask'):
277+
signal.pthread_sigmask(signal.SIG_BLOCK, range(1, signal.NSIG))
254278
while 1:
255279
try:
256280
conn = self._listener.accept()
257-
key, destination_pid = conn.recv()
281+
msg = conn.recv()
282+
if msg is None:
283+
break
284+
key, destination_pid = msg
258285
send, close = self._cache.pop(key)
259286
send(conn, destination_pid)
260287
close()

Lib/test/test_multiprocessing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,11 @@ class _TestPicklingConnections(BaseTestCase):
19651965

19661966
ALLOWED_TYPES = ('processes',)
19671967

1968+
@classmethod
1969+
def tearDownClass(cls):
1970+
from multiprocessing.reduction import resource_sharer
1971+
resource_sharer.stop(timeout=5)
1972+
19681973
@classmethod
19691974
def _listener(cls, conn, families):
19701975
for fam in families:

0 commit comments

Comments
 (0)