From 4bb08c671bbaef5f4e5c24e4f7a5c7f1d62f495d Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Wed, 26 Jun 2019 13:39:58 +0300 Subject: [PATCH] Fixed race condition when rpc server responds too fast --- json_rpc/clients/socketclient.nim | 8 +++++--- json_rpc/clients/websocketclient.nim | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/json_rpc/clients/socketclient.nim b/json_rpc/clients/socketclient.nim index 1a8064a..4357d9a 100644 --- a/json_rpc/clients/socketclient.nim +++ b/json_rpc/clients/socketclient.nim @@ -21,14 +21,16 @@ method call*(self: RpcSocketClient, name: string, if self.transport.isNil: raise newException(ValueError, "Transport is not initialised (missing a call to connect?)") - let res = await self.transport.write(value) - # TODO: Add actions when not full packet was send, e.g. disconnect peer. - doAssert(res == len(value)) # completed by processMessage. var newFut = newFuture[Response]() # add to awaiting responses self.awaiting[id] = newFut + + let res = await self.transport.write(value) + # TODO: Add actions when not full packet was send, e.g. disconnect peer. + doAssert(res == len(value)) + result = await newFut proc processData(client: RpcSocketClient) {.async.} = diff --git a/json_rpc/clients/websocketclient.nim b/json_rpc/clients/websocketclient.nim index 714e147..1bb4183 100644 --- a/json_rpc/clients/websocketclient.nim +++ b/json_rpc/clients/websocketclient.nim @@ -23,12 +23,13 @@ method call*(self: RpcWebSocketClient, name: string, raise newException(ValueError, "Transport is not initialised (missing a call to connect?)") # echo "Sent msg: ", value - await self.transport.send(value) # completed by processMessage. var newFut = newFuture[Response]() # add to awaiting responses self.awaiting[id] = newFut + + await self.transport.send(value) result = await newFut proc processData(client: RpcWebSocketClient) {.async.} =