From 94f37b764becd9d55ce9801ff72365c79d33b7c8 Mon Sep 17 00:00:00 2001 From: ghassmo Date: Fri, 13 May 2022 10:53:03 +0300 Subject: [PATCH] rpcclient: handle error when lost connection with rpc server --- src/rpc/rpcclient.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/rpc/rpcclient.rs b/src/rpc/rpcclient.rs index 4905293bb..83039375d 100644 --- a/src/rpc/rpcclient.rs +++ b/src/rpc/rpcclient.rs @@ -1,7 +1,7 @@ use async_std::sync::Arc; use async_executor::Executor; -use log::debug; +use log::{debug, error}; use serde_json::{json, Value}; use url::Url; @@ -27,17 +27,26 @@ impl RpcClient { self.sender.send(value).await?; - let reply: JsonResult = self.receiver.recv().await?; + let reply = self.receiver.recv().await; - match reply { + // if the connection is closed the receiver will get an error + // for waiting closed channel + if reply.is_err() { + error!("Unable to connect to the RPC server"); + return Err(Error::OperationFailed) + } + + match reply? { JsonResult::Resp(r) => { // check if the ids match let resp_id = r.id.as_u64(); + if resp_id.is_none() { let error = jsonrpc::error(ErrorCode::InvalidId, None, r.id); self.stop_signal.send(()).await?; return Err(Error::JsonRpcError(error.error.message.to_string())) } + if resp_id.unwrap() != req_id { let error = jsonrpc::error( ErrorCode::InvalidId,