Use fake RPC when import lock is held
When the application is importing modules, an API call can cause
a deadlock if it performs any imports in the RPC thread. See
docs.python.org/2/library/threading.html#importing-in-threaded-code
@@ -66,6 +66,7 @@ A few caveats:
import google
+import imp
import os
import pickle
import random
@@ -250,6 +251,12 @@ class RuntimeRemoteStub(RemoteStub):
def CreateRPC(self):
""" Create an RPC that can be used asynchronously. """
+ # If the runtime is importing a module, fall back to the non-threaded RPC.
+ # This prevents a deadlock in cases when the RealRPC thread tries to
+ # acquire the import lock.
+ if imp.lock_held():
+ return apiproxy_rpc.RPC(stub=self)
+ else:
return apiproxy_rpc.RealRPC(stub=self)
@@ -538,6 +545,12 @@ class RuntimeDatastoreStub(RemoteDatastoreStub):
def CreateRPC(self):
""" Create an RPC that can be used asynchronously. """
+ # If the runtime is importing a module, fall back to the non-threaded RPC.
+ # This prevents a deadlock in cases when the RealRPC thread tries to
+ # acquire the import lock.
+ if imp.lock_held():
+ return apiproxy_rpc.RPC(stub=self)
+ else:
return apiproxy_rpc.RealRPC(stub=self)
