Skip to content

Commit 2a8567b

Browse files
committed
core: serialize calls to _service_stub_main().
See comment.
1 parent cf8ecf1 commit 2a8567b

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

mitogen/core.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3136,10 +3136,21 @@ def _on_broker_exit(self):
31363136
if not self.config['profiling']:
31373137
os.kill(os.getpid(), signal.SIGTERM)
31383138

3139+
#: On Python >3.4, the global importer lock has been sharded into a
3140+
#: per-module lock, meaning there is no guarantee the import statement in
3141+
#: service_stub_main will be truly complete before a second thread
3142+
#: attempting the same import will see a partially initialized module.
3143+
#: Sigh. Therefore serialize execution of the stub itself.
3144+
service_stub_lock = threading.Lock()
3145+
31393146
def _service_stub_main(self, msg):
3140-
import mitogen.service
3141-
pool = mitogen.service.get_or_create_pool(router=self.router)
3142-
pool._receiver._on_receive(msg)
3147+
self.service_stub_lock.acquire()
3148+
try:
3149+
import mitogen.service
3150+
pool = mitogen.service.get_or_create_pool(router=self.router)
3151+
pool._receiver._on_receive(msg)
3152+
finally:
3153+
self.service_stub_lock.release()
31433154

31443155
def _on_call_service_msg(self, msg):
31453156
"""

mitogen/fork.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def on_fork():
9898
fixup_prngs()
9999
mitogen.core.Latch._on_fork()
100100
mitogen.core.Side._on_fork()
101+
mitogen.core.ExternalContext.service_stub_lock = threading.Lock()
101102

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

0 commit comments

Comments
 (0)