From a8b83281abbb40ed36f4290ba35a4daaafa798c8 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Thu, 2 Jul 2009 18:10:13 +0000 Subject: [PATCH] If a exported method returns a deferred, the rpcserver will now wait for it to fire before sending either the RPC_RESPONSE or RPC_ERROR message back to the client --- deluge/core/rpcserver.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/deluge/core/rpcserver.py b/deluge/core/rpcserver.py index 7c3810e28..c858599c5 100644 --- a/deluge/core/rpcserver.py +++ b/deluge/core/rpcserver.py @@ -42,7 +42,7 @@ import stat import traceback from twisted.internet.protocol import Factory, Protocol -from twisted.internet import ssl, reactor +from twisted.internet import ssl, reactor, defer from OpenSSL import crypto, SSL from types import FunctionType @@ -272,7 +272,23 @@ class DelugeRPCProtocol(Protocol): if not isinstance(e, DelugeError): log.exception("Exception calling RPC request: %s", e) else: - self.sendData((RPC_RESPONSE, request_id, ret)) + # Check if the return value is a deferred, since we'll need to + # wait for it to fire before sending the RPC_RESPONSE + if isinstance(ret, defer.Deferred): + def on_success(result): + self.sendData((RPC_RESPONSE, request_id, ret)) + return result + + def on_fail(failure): + try: + failure.raiseException() + except Exception, e: + sendError() + return failure + + ret.addCallbacks(on_success, on_fail) + else: + self.sendData((RPC_RESPONSE, request_id, ret)) class RPCServer(component.Component): """