Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/tool/subcommands/api_cmd/generate_test_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,17 @@ async fn ctx(
read_genesis_header(None, chain_config.genesis_bytes(&db).await?.as_deref(), &db).await?;
let chain_store = ChainStore::new(db.clone(), chain_config, genesis_header)?;
let state_manager = StateManager::new(chain_store.shallow_clone())?;
let mut services: JoinSet<anyhow::Result<()>> = JoinSet::new();
let message_pool = MessagePool::new(
chain_store,
network_send.clone(),
Default::default(),
state_manager.chain_config().clone(),
&mut JoinSet::new(),
&mut services,
)?;
// See `super::test_snapshot::drain_mpool_services` for rationale.
services.abort_all();
tokio::spawn(super::test_snapshot::drain_mpool_services(services));

let peer_manager = Arc::new(PeerManager::default());
let sync_network_context =
Expand Down
25 changes: 24 additions & 1 deletion src/tool/subcommands/api_cmd/test_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,21 @@ async fn ctx(
read_genesis_header(None, chain_config.genesis_bytes(&db).await?.as_deref(), &db).await?;
let chain_store = ChainStore::new(db, chain_config, genesis_header.clone())?;
let state_manager = StateManager::new(chain_store.shallow_clone()).unwrap();
let mut services: JoinSet<anyhow::Result<()>> = JoinSet::new();
let message_pool = MessagePool::new(
chain_store,
network_send.clone(),
Default::default(),
state_manager.chain_config().clone(),
&mut JoinSet::new(),
&mut services,
)?;
// The mpool services are not needed in this snapshot test context; abort
// them right away so they don't compete with the test for runtime time
// (the inherited `&mut JoinSet::new()` pattern was a temporary that
// dropped — same end state). The detached drain still polls the aborted
// set so any pre-abort error or panic is surfaced rather than dropped.
services.abort_all();
tokio::spawn(drain_mpool_services(services));

let peer_manager = Arc::new(PeerManager::default());
let sync_network_context =
Expand All @@ -176,6 +184,21 @@ async fn ctx(
Ok((rpc_state, network_rx, shutdown_recv))
}

/// Drains a `MessagePool` service [`JoinSet`] to completion, logging any
/// errors or panics it produces. Intended to be used with `tokio::spawn` from
/// test-utility `ctx()` helpers so that service-task errors are surfaced
/// instead of being silently dropped when the `JoinSet` is dropped.
pub(super) async fn drain_mpool_services(mut services: JoinSet<anyhow::Result<()>>) {
while let Some(result) = services.join_next().await {
match result {
Ok(Ok(())) => {}
Ok(Err(e)) => tracing::warn!("message pool service task error: {e:#}"),
Err(je) if je.is_cancelled() => {}
Err(je) => tracing::warn!("message pool service task panicked: {je}"),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down