diff --git a/tornadorpc/base.py b/tornadorpc/base.py index e7935ea..d865d16 100644 --- a/tornadorpc/base.py +++ b/tornadorpc/base.py @@ -140,7 +140,21 @@ def dispatch(self, method_name, params): self.traceback(method_name, params) return self.handler.result(self.faults.internal_error()) - if getattr(method, 'async', False): + from tornado.concurrent import Future + if isinstance(response, Future): + # self.handler is actually a transient: BaseRPCParser is used as a + # singleton class where self.handler changes each time a request is + # processed, so we need to treat it as a temporary, that may change + # anytime control flow returns to Tornado's IO handler + def make_completer(handler): + def completer(future): + handler.result(future.result()) + return completer + if response.done(): + return self.handler.result(response.result()) + else: + tornado.ioloop.IOLoop.instance().add_future(response, make_completer(self.handler)) + elif getattr(method, 'async', False): # Asynchronous response -- the method should have called # self.result(RESULT_VALUE) if response is not None: