Skip to content

Commit

Permalink
server: fix python rpc kill handling
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Mar 17, 2023
1 parent d564cf1 commit 9ba22e4
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions server/python/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class RPCResultError(Exception):
def __init__(self, caught, message):
self.caught = caught
self.message = message
self.name = None
self.stack = None


class RpcSerializer:
Expand Down Expand Up @@ -121,6 +123,16 @@ def __init__(self, send: Callable[[object, Callable[[Exception], None], Dict], N
self.killed = False

def __apply__(self, proxyId: str, oneWayMethods: List[str], method: str, args: list):
oneway = oneWayMethods and method in oneWayMethods

if self.killed:
future = Future()
if oneway:
future.set_result(None)
return future
future.set_exception(RPCResultError(None, 'RpcPeer has been killed (apply) ' + str(method)))
return future

serializationContext: Dict = {}
serializedArgs = []
for arg in args:
Expand All @@ -134,7 +146,7 @@ def __apply__(self, proxyId: str, oneWayMethods: List[str], method: str, args: l
'method': method,
}

if oneWayMethods and method in oneWayMethods:
if oneway:
rpcApply['oneway'] = True
self.send(rpcApply, None, serializationContext)
future = Future()
Expand Down Expand Up @@ -472,12 +484,13 @@ async def handleMessage(self, message: Dict, deserializationContext: Dict):
pass

async def createPendingResult(self, cb: Callable[[str, Callable[[Exception], None]], None]):
# if (Object.isFrozen(this.pendingResults))
# return Promise.reject(new RPCResultError('RpcPeer has been killed'));
future = Future()
if self.killed:
future.set_exception(RPCResultError(None, 'RpcPeer has been killed (createPendingResult)'))
return future

id = str(self.idCounter)
self.idCounter = self.idCounter + 1
future = Future()
self.pendingResults[id] = future
await cb(id, lambda e: future.set_exception(RPCResultError(e, None)))
return await future
Expand Down

0 comments on commit 9ba22e4

Please sign in to comment.