File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 """
Original file line number Diff line number Diff 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 :
You can’t perform that action at this time.
0 commit comments