unix: ensure mitogen.context_id is reset when client disconnects

To ensure a test process can successfully recreate an Ansible
MuxProcess, reset fork-inherited globals during disconnection.

There is basically no good place for this. Per the comments on #91, it
would be far better if the context's identity was tied to its router,
rather than some global variable.
pull/607/head
David Wilson 5 years ago
parent 7c4621a010
commit 6e33de7cd2

@ -172,6 +172,7 @@ def _connect(path, broker, sock):
stream.receive_side = side
stream.name = u'unix_listener.%d' % (pid,)
mitogen.core.listen(stream, 'disconnect', _cleanup)
mitogen.core.listen(router.broker, 'shutdown',
lambda: router.disconnect_stream(stream))
@ -179,6 +180,7 @@ def _connect(path, broker, sock):
router.register(context, stream)
return router, context
def connect(path, broker=None):
LOG.debug('unix.connect(path=%r)', path)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
@ -187,3 +189,14 @@ def connect(path, broker=None):
except:
sock.close()
raise
def _cleanup():
"""
Reset mitogen.context_id and friends when our connection to the parent is
lost. Per comments on #91, these globals need to move to the Router so
fix-ups like this become unnecessary.
"""
mitogen.context_id = 0
mitogen.parent_id = None
mitogen.parent_ids = []

Loading…
Cancel
Save