Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit 408393c

Browse files
authored
fix(merkle-tree): Change LazyAsyncTreeReader::wait() signature (matter-labs#2314)
## What ❔ Makes `LazyAsyncTreeReader::wait()` return an `Option` to make it clearer that not initializing is not necessarily an error (this decision is up to the caller). ## Why ❔ Recent errors on ENs were caused by unwrapping `LazyAsyncTreeReader::wait()`. They partially masked the real error cause (not related to this task). ## Checklist - [x] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [x] Documentation comments have been added / updated. - [x] Code has been formatted via `zk fmt` and `zk lint`.
1 parent 7b3877f commit 408393c

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

core/bin/external_node/src/main.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,19 @@ async fn run_tree(
185185
if let Some(api_config) = api_config {
186186
let address = (Ipv4Addr::UNSPECIFIED, api_config.port).into();
187187
let tree_reader = metadata_calculator.tree_reader();
188-
let stop_receiver = stop_receiver.clone();
188+
let mut stop_receiver = stop_receiver.clone();
189189
task_futures.push(tokio::spawn(async move {
190-
tree_reader
191-
.wait()
192-
.await
193-
.context("Cannot initialize tree reader")?
194-
.run_api_server(address, stop_receiver)
195-
.await
190+
if let Some(reader) = tree_reader.wait().await {
191+
reader.run_api_server(address, stop_receiver).await
192+
} else {
193+
// Tree is dropped before initialized, e.g. because the node is getting shut down.
194+
// We don't want to treat this as an error since it could mask the real shutdown cause in logs etc.
195+
tracing::warn!(
196+
"Tree is dropped before initialized, not starting the tree API server"
197+
);
198+
stop_receiver.changed().await?;
199+
Ok(())
200+
}
196201
}));
197202
}
198203

core/node/metadata_calculator/src/helpers.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ impl MerkleTreeHealthCheck {
9191
let weak_reader = Arc::<OnceCell<WeakAsyncTreeReader>>::default();
9292
let weak_reader_for_task = weak_reader.clone();
9393
tokio::spawn(async move {
94-
weak_reader_for_task
95-
.set(reader.wait().await.unwrap().downgrade())
96-
.ok();
94+
if let Some(reader) = reader.wait().await {
95+
weak_reader_for_task.set(reader.downgrade()).ok();
96+
}
97+
// Otherwise, the tree is dropped before getting initialized; this is not an error in this context.
9798
});
9899

99100
Self {
@@ -393,16 +394,14 @@ impl LazyAsyncTreeReader {
393394
self.0.borrow().clone()
394395
}
395396

396-
/// Waits until the tree is initialized and returns a reader for it.
397-
pub async fn wait(mut self) -> anyhow::Result<AsyncTreeReader> {
397+
/// Waits until the tree is initialized and returns a reader for it. If the tree is dropped before
398+
/// getting initialized, returns `None`.
399+
pub async fn wait(mut self) -> Option<AsyncTreeReader> {
398400
loop {
399401
if let Some(reader) = self.0.borrow().clone() {
400-
break Ok(reader);
402+
break Some(reader);
401403
}
402-
self.0
403-
.changed()
404-
.await
405-
.context("Tree dropped without getting ready; not resolving tree reader")?;
404+
self.0.changed().await.ok()?;
406405
}
407406
}
408407
}

core/node/node_framework/src/implementations/layers/metadata_calculator.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,16 @@ impl Task for TreeApiTask {
162162
"tree_api".into()
163163
}
164164

165-
async fn run(self: Box<Self>, stop_receiver: StopReceiver) -> anyhow::Result<()> {
166-
self.tree_reader
167-
.wait()
168-
.await
169-
.context("Cannot initialize tree reader")?
170-
.run_api_server(self.bind_addr, stop_receiver.0)
171-
.await
165+
async fn run(self: Box<Self>, mut stop_receiver: StopReceiver) -> anyhow::Result<()> {
166+
if let Some(reader) = self.tree_reader.wait().await {
167+
reader.run_api_server(self.bind_addr, stop_receiver.0).await
168+
} else {
169+
// Tree is dropped before initialized, e.g. because the node is getting shut down.
170+
// We don't want to treat this as an error since it could mask the real shutdown cause in logs etc.
171+
tracing::warn!("Tree is dropped before initialized, not starting the tree API server");
172+
stop_receiver.0.changed().await?;
173+
Ok(())
174+
}
172175
}
173176
}
174177

0 commit comments

Comments
 (0)