@ -36,6 +36,11 @@ import socket
import sys
import sys
import time
import time
try :
import faulthandler
except ImportError :
faulthandler = None
import mitogen
import mitogen
import mitogen . core
import mitogen . core
import mitogen . debug
import mitogen . debug
@ -70,6 +75,17 @@ def clean_shutdown(sock):
sock . recv ( 1 )
sock . recv ( 1 )
def getenv_int ( key , default = 0 ) :
"""
Get an integer - valued environment variable ` key ` , if it exists and parses
as an integer , otherwise return ` default ` .
"""
try :
return int ( os . environ . get ( key , str ( default ) ) )
except ValueError :
return default
class MuxProcess ( object ) :
class MuxProcess ( object ) :
"""
"""
Implement a subprocess forked from the Ansible top - level , as a safe place
Implement a subprocess forked from the Ansible top - level , as a safe place
@ -127,6 +143,9 @@ class MuxProcess(object):
if cls . worker_sock is not None :
if cls . worker_sock is not None :
return
return
if faulthandler is not None :
faulthandler . enable ( )
cls . unix_listener_path = mitogen . unix . make_socket_path ( )
cls . unix_listener_path = mitogen . unix . make_socket_path ( )
cls . worker_sock , cls . child_sock = socket . socketpair ( )
cls . worker_sock , cls . child_sock = socket . socketpair ( )
atexit . register ( lambda : clean_shutdown ( cls . worker_sock ) )
atexit . register ( lambda : clean_shutdown ( cls . worker_sock ) )
@ -164,6 +183,15 @@ class MuxProcess(object):
# Block until the socket is closed, which happens on parent exit.
# Block until the socket is closed, which happens on parent exit.
mitogen . core . io_op ( self . child_sock . recv , 1 )
mitogen . core . io_op ( self . child_sock . recv , 1 )
def _enable_router_debug ( self ) :
if ' MITOGEN_ROUTER_DEBUG ' in os . environ :
self . router . enable_debug ( )
def _enable_stack_dumps ( self ) :
secs = getenv_int ( ' MITOGEN_DUMP_THREAD_STACKS ' , default = 0 )
if secs :
mitogen . debug . dump_to_logger ( secs = secs )
def _setup_master ( self ) :
def _setup_master ( self ) :
"""
"""
Construct a Router , Broker , and mitogen . unix listener
Construct a Router , Broker , and mitogen . unix listener
@ -177,10 +205,8 @@ class MuxProcess(object):
router = self . router ,
router = self . router ,
path = self . unix_listener_path ,
path = self . unix_listener_path ,
)
)
if ' MITOGEN_ROUTER_DEBUG ' in os . environ :
self . _enable_router_debug ( )
self . router . enable_debug ( )
self . _enable_stack_dumps ( )
if ' MITOGEN_DUMP_THREAD_STACKS ' in os . environ :
mitogen . debug . dump_to_logger ( )
def _setup_services ( self ) :
def _setup_services ( self ) :
"""
"""
@ -195,7 +221,7 @@ class MuxProcess(object):
ansible_mitogen . services . ContextService ( self . router ) ,
ansible_mitogen . services . ContextService ( self . router ) ,
ansible_mitogen . services . ModuleDepService ( self . router ) ,
ansible_mitogen . services . ModuleDepService ( self . router ) ,
] ,
] ,
size = int ( os . environ . get ( ' MITOGEN_POOL_SIZE ' , ' 16 ' ) ) ,
size = getenv_int ( ' MITOGEN_POOL_SIZE ' , default = 16 ) ,
)
)
LOG . debug ( ' Service pool configured: size= %d ' , self . pool . size )
LOG . debug ( ' Service pool configured: size= %d ' , self . pool . size )