Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(merkle-tree): Change LazyAsyncTreeReader::wait() signature #2314

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions core/bin/external_node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,19 @@ async fn run_tree(
if let Some(api_config) = api_config {
let address = (Ipv4Addr::UNSPECIFIED, api_config.port).into();
let tree_reader = metadata_calculator.tree_reader();
let stop_receiver = stop_receiver.clone();
let mut stop_receiver = stop_receiver.clone();
task_futures.push(tokio::spawn(async move {
tree_reader
.wait()
.await
.context("Cannot initialize tree reader")?
.run_api_server(address, stop_receiver)
.await
if let Some(reader) = tree_reader.wait().await {
reader.run_api_server(address, stop_receiver).await
} else {
// Tree is dropped before initialized, e.g. because the node is getting shut down.
// We don't want to treat this as an error since it could mask the real shutdown cause in logs etc.
tracing::warn!(
"Tree is dropped before initialized, not starting the tree API server"
);
stop_receiver.changed().await?;
Ok(())
}
}));
}

Expand Down
19 changes: 9 additions & 10 deletions core/node/metadata_calculator/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ impl MerkleTreeHealthCheck {
let weak_reader = Arc::<OnceCell<WeakAsyncTreeReader>>::default();
let weak_reader_for_task = weak_reader.clone();
tokio::spawn(async move {
weak_reader_for_task
.set(reader.wait().await.unwrap().downgrade())
.ok();
if let Some(reader) = reader.wait().await {
weak_reader_for_task.set(reader.downgrade()).ok();
}
// Otherwise, the tree is dropped before getting initialized; this is not an error in this context.
});

Self {
Expand Down Expand Up @@ -393,16 +394,14 @@ impl LazyAsyncTreeReader {
self.0.borrow().clone()
}

/// Waits until the tree is initialized and returns a reader for it.
pub async fn wait(mut self) -> anyhow::Result<AsyncTreeReader> {
/// Waits until the tree is initialized and returns a reader for it. If the tree is dropped before
/// getting initialized, returns `None`.
pub async fn wait(mut self) -> Option<AsyncTreeReader> {
loop {
if let Some(reader) = self.0.borrow().clone() {
break Ok(reader);
break Some(reader);
}
self.0
.changed()
.await
.context("Tree dropped without getting ready; not resolving tree reader")?;
self.0.changed().await.ok()?;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,16 @@ impl Task for TreeApiTask {
"tree_api".into()
}

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

Expand Down
Loading