Commit f6c24ab6 authored by David Wilson's avatar David Wilson

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.
parent 67e0a4fe
......@@ -67,6 +67,7 @@ class Stream(mitogen.parent.Stream):
def create_child(self, *_args):
parentfp, childfp = mitogen.parent.create_socketpair()
self.pid = os.fork()
self.name = 'fork.' + str(self.pid)
if self.pid:
childfp.close()
# Decouple the socket from the lifetime of the Python socket object.
......@@ -78,6 +79,8 @@ class Stream(mitogen.parent.Stream):
self._child_main(childfp)
def _child_main(self, childfp):
# TODO: Latch descriptors inherited from the parent should be closed.
vars(mitogen.core._tls).clear()
break_logging_locks()
mitogen.core.set_block(childfp.fileno())
os.dup2(childfp.fileno(), 1)
......@@ -89,10 +92,6 @@ class Stream(mitogen.parent.Stream):
mitogen.core.ExternalContext().main(**kwargs)
sys.exit(0)
def connect(self):
super(Stream, self).connect()
self.name = 'fork.' + str(self.pid)
def _connect_bootstrap(self):
# None required.
pass
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment