Skip to content

Commit

Permalink
fix: avoid deadlock when replacing blockchain client websocket connec…
Browse files Browse the repository at this point in the history
…tion (#970)
  • Loading branch information
dinhani-cw authored Jun 3, 2024
1 parent 7b3d8a1 commit b5a5661
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 2 additions & 0 deletions config/importer-online.env.local
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
RUST_LOG=importer_online=info,stratus=info

CHAIN_ID=2008
EVMS=1

Expand Down
9 changes: 5 additions & 4 deletions src/infra/blockchain_client/blockchain_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ impl BlockchainClient {

pub async fn subscribe_new_heads(&self) -> anyhow::Result<Subscription<ExternalBlock>> {
tracing::debug!("subscribing to newHeads event");
let ws = self.require_ws().await?;

let mut first_attempt = true;
loop {
let result = ws
let ws_read = self.require_ws().await?;
let result = ws_read
.subscribe::<ExternalBlock, Vec<JsonValue>>("eth_subscribe", vec![JsonValue::String("newHeads".to_owned())], "eth_unsubscribe")
.await;

Expand All @@ -274,8 +274,9 @@ impl BlockchainClient {

// reconnect websocket client
let new_ws_client = Self::build_ws_client(self.ws_url.as_ref().unwrap(), self.timeout).await?;
let mut current_ws_client = self.ws.as_ref().unwrap().write().await;
let _ = std::mem::replace(&mut *current_ws_client, new_ws_client);
drop(ws_read);
let mut ws_write = self.ws.as_ref().unwrap().write().await;
let _ = std::mem::replace(&mut *ws_write, new_ws_client);
}

// failed and cannot do anything
Expand Down

0 comments on commit b5a5661

Please sign in to comment.