unix: don't crash listener if remote end disconnects.

In some scenarios, Ansible's worker seems to exit early, resulting in
EPIPE during .recv() or .send(). Log an error and gracefully disconnect
in that case.
pull/372/head
David Wilson 6 years ago
parent 6c8a667691
commit 870bbe0eae

@ -78,21 +78,38 @@ class Listener(mitogen.core.BasicStream):
self.receive_side = mitogen.core.Side(self, self._sock.fileno()) self.receive_side = mitogen.core.Side(self, self._sock.fileno())
router.broker.start_receive(self) router.broker.start_receive(self)
def on_receive(self, broker): def _accept_client(self, sock):
sock, _ = self._sock.accept()
sock.setblocking(True) sock.setblocking(True)
try:
pid, = struct.unpack('>L', sock.recv(4)) pid, = struct.unpack('>L', sock.recv(4))
except socket.error:
LOG.error('%r: failed to read remote identity: %s',
self, sys.exc_info()[1])
return
context_id = self._router.id_allocator.allocate() context_id = self._router.id_allocator.allocate()
context = mitogen.parent.Context(self._router, context_id) context = mitogen.parent.Context(self._router, context_id)
stream = mitogen.core.Stream(self._router, context_id) stream = mitogen.core.Stream(self._router, context_id)
stream.accept(sock.fileno(), sock.fileno())
stream.name = u'unix_client.%d' % (pid,) stream.name = u'unix_client.%d' % (pid,)
stream.auth_id = mitogen.context_id stream.auth_id = mitogen.context_id
stream.is_privileged = True stream.is_privileged = True
self._router.register(context, stream)
try:
sock.send(struct.pack('>LLL', context_id, mitogen.context_id, sock.send(struct.pack('>LLL', context_id, mitogen.context_id,
os.getpid())) os.getpid()))
except socket.error:
LOG.error('%r: failed to assign identity to PID %d: %s',
self, pid, sys.exc_info()[1])
return
stream.accept(sock.fileno(), sock.fileno())
self._router.register(context, stream)
def on_receive(self, broker):
sock, _ = self._sock.accept()
try:
self._accept_client(sock)
finally:
sock.close() sock.close()

Loading…
Cancel
Save