From c0ced6d04a7b09a69ccde0e2280cf2f209656f80 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 29 Apr 2018 06:15:23 +0100 Subject: [PATCH] core: fix monster fork FD leak _sockets only refers to the idle sockets list, it doesn't refer to every socket currently in use by a Latch, for example, the 2*16 used by e.g. Ansible's sleeping service pool. --- mitogen/core.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mitogen/core.py b/mitogen/core.py index 1bb4a1ee..2e43ed65 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -981,6 +981,7 @@ class Latch(object): closed = False _waking = 0 _sockets = [] + _allsockets = [] def __init__(self): self._lock = threading.Lock() @@ -989,10 +990,9 @@ class Latch(object): @classmethod def _on_fork(cls): - while cls._sockets: - rsock, wsock = cls._sockets.pop() - rsock.close() - wsock.close() + cls._sockets = [] + while cls._allsockets: + cls._allsockets.pop().close() def close(self): self._lock.acquire() @@ -1015,6 +1015,7 @@ class Latch(object): rsock, wsock = socket.socketpair() set_cloexec(rsock.fileno()) set_cloexec(wsock.fileno()) + self._allsockets.extend((rsock, wsock)) return rsock, wsock def get(self, timeout=None, block=True):