Skip to content

Commit

Permalink
core: serialize calls to _service_stub_main().
Browse files Browse the repository at this point in the history
See comment.
  • Loading branch information
dw committed Feb 13, 2019
1 parent cf8ecf1 commit 2a8567b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
17 changes: 14 additions & 3 deletions mitogen/core.py
Expand Up @@ -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):
"""
Expand Down
1 change: 1 addition & 0 deletions mitogen/fork.py
Expand Up @@ -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:
Expand Down

0 comments on commit 2a8567b

Please sign in to comment.