issue #155: don't inherit TLS state in mitogen.fork

This is a partial fix to a general problem: deciding which bits of state
to keep from the parent, and which to clear out. When forking from a
heavily threaded process, there will be 2x$n_threads fds just sitting
around doing nothing, due to Latch use in the parent.

We can't just close all nonstandard fds post-fork, since user code may
be expecting some FDs to be preserved.
pull/167/head
David Wilson 7 years ago
parent 67e0a4fe59
commit f6c24ab615

@ -67,6 +67,7 @@ class Stream(mitogen.parent.Stream):
def create_child(self, *_args): def create_child(self, *_args):
parentfp, childfp = mitogen.parent.create_socketpair() parentfp, childfp = mitogen.parent.create_socketpair()
self.pid = os.fork() self.pid = os.fork()
self.name = 'fork.' + str(self.pid)
if self.pid: if self.pid:
childfp.close() childfp.close()
# Decouple the socket from the lifetime of the Python socket object. # Decouple the socket from the lifetime of the Python socket object.
@ -78,6 +79,8 @@ class Stream(mitogen.parent.Stream):
self._child_main(childfp) self._child_main(childfp)
def _child_main(self, childfp): def _child_main(self, childfp):
# TODO: Latch descriptors inherited from the parent should be closed.
vars(mitogen.core._tls).clear()
break_logging_locks() break_logging_locks()
mitogen.core.set_block(childfp.fileno()) mitogen.core.set_block(childfp.fileno())
os.dup2(childfp.fileno(), 1) os.dup2(childfp.fileno(), 1)
@ -89,10 +92,6 @@ class Stream(mitogen.parent.Stream):
mitogen.core.ExternalContext().main(**kwargs) mitogen.core.ExternalContext().main(**kwargs)
sys.exit(0) sys.exit(0)
def connect(self):
super(Stream, self).connect()
self.name = 'fork.' + str(self.pid)
def _connect_bootstrap(self): def _connect_bootstrap(self):
# None required. # None required.
pass pass

Loading…
Cancel
Save