Skip to content
Permalink
Browse files

core: serialize calls to _service_stub_main().

See comment.
  • Loading branch information...
dw committed Feb 13, 2019
1 parent cf8ecf1 commit 2a8567b4327bd7d67eb835e71a7d7af2ec564cc3
Showing with 15 additions and 3 deletions.
  1. +14 −3 mitogen/core.py
  2. +1 −0 mitogen/fork.py
@@ -3136,10 +3136,21 @@ def _on_broker_exit(self):
if not self.config['profiling']:
os.kill(os.getpid(), signal.SIGTERM)

#: On Python >3.4, the global importer lock has been sharded into a
#: per-module lock, meaning there is no guarantee the import statement in
#: service_stub_main will be truly complete before a second thread
#: attempting the same import will see a partially initialized module.
#: Sigh. Therefore serialize execution of the stub itself.
service_stub_lock = threading.Lock()

def _service_stub_main(self, msg):
import mitogen.service
pool = mitogen.service.get_or_create_pool(router=self.router)
pool._receiver._on_receive(msg)
self.service_stub_lock.acquire()
try:
import mitogen.service
pool = mitogen.service.get_or_create_pool(router=self.router)
pool._receiver._on_receive(msg)
finally:
self.service_stub_lock.release()

def _on_call_service_msg(self, msg):
"""
@@ -98,6 +98,7 @@ def on_fork():
fixup_prngs()
mitogen.core.Latch._on_fork()
mitogen.core.Side._on_fork()
mitogen.core.ExternalContext.service_stub_lock = threading.Lock()

mitogen__service = sys.modules.get('mitogen.service')
if mitogen__service:

0 comments on commit 2a8567b

Please sign in to comment.
You can’t perform that action at this time.