From 33d96b9f320c4415d8f65634f163868620c84cf7 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 27 Nov 2023 19:21:26 +0100 Subject: [PATCH] Fix panic when all chains are removed (#1422) * Fix panic when all chains are removed * PR link --- light-base/src/network_service.rs | 16 +++++++--------- wasm-node/CHANGELOG.md | 4 ++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/light-base/src/network_service.rs b/light-base/src/network_service.rs index 449fd35638..5ab9b8d891 100644 --- a/light-base/src/network_service.rs +++ b/light-base/src/network_service.rs @@ -978,16 +978,14 @@ async fn background_task(mut task: BackgroundTask) { .map_or(WakeUpReason::ForegroundClosed, WakeUpReason::Message) }; let message_for_chain_received = async { - if !task.messages_rx.is_empty() { - let (chain_id, message) = task - .messages_rx - .next() - .await - .unwrap_or_else(|| unreachable!()); - WakeUpReason::MessageForChain(chain_id, message) - } else { + // Note that when the last entry of `messages_rx` yields `None`, `messages_rx` + // itself will yield `None`. For this reason, we can't use + // `task.messages_rx.is_empty()` to determine whether `messages_rx` will + // yield `None`. + let Some((chain_id, message)) = task.messages_rx.next().await else { future::pending().await - } + }; + WakeUpReason::MessageForChain(chain_id, message) }; let message_from_task_received = async { let (connection_id, message) = task.tasks_messages_rx.next().await.unwrap(); diff --git a/wasm-node/CHANGELOG.md b/wasm-node/CHANGELOG.md index 7b80407a9f..4b9bd24df9 100644 --- a/wasm-node/CHANGELOG.md +++ b/wasm-node/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Fixed + +- Fix panic in network service when all chains are removed. ([#1422](https://github.com/smol-dot/smoldot/pull/1422)) + ## 2.0.11 - 2023-11-27 ### Changed