From 872181bebdc6039de1ed0070e5b7d62b6e95b784 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sun, 25 Mar 2018 12:38:08 +0545 Subject: [PATCH] issue #155: core: implement Side._on_fork() Central mechanism for deleting all non-Latch file descriptors belonging to the parent process during fork(). --- mitogen/core.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mitogen/core.py b/mitogen/core.py index 2b11bed1..52fd79a9 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -45,6 +45,7 @@ import threading import time import traceback import warnings +import weakref import zlib # TODO: usage of 'import' after setting __name__, but before fixing up @@ -658,10 +659,13 @@ class LogHandler(logging.Handler): class Side(object): + _fork_refs = weakref.WeakValueDictionary() + def __init__(self, stream, fd, cloexec=True, keep_alive=True): self.stream = stream self.fd = fd self.keep_alive = keep_alive + self._fork_refs[id(self)] = self if cloexec: set_cloexec(fd) set_nonblock(fd) @@ -674,6 +678,11 @@ class Side(object): raise StreamError('%r.fileno() called but no FD set', self) return self.fd + @classmethod + def _on_fork(cls): + for side in list(cls._fork_refs.values()): + side.close() + def close(self): if self.fd is not None: _vv and IOLOG.debug('%r.close()', self)