From 9d2626a9882ffbd31b1fb4ae9bcad90f53b75fe2 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 26 Sep 2022 10:26:37 +0200 Subject: [PATCH 1/2] Clean-up runtimes management in runtime_service --- bin/light-base/src/runtime_service.rs | 20 +++++++++++++++----- bin/wasm-node/CHANGELOG.md | 4 ++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/bin/light-base/src/runtime_service.rs b/bin/light-base/src/runtime_service.rs index 0fbd685805..dc255f4ae8 100644 --- a/bin/light-base/src/runtime_service.rs +++ b/bin/light-base/src/runtime_service.rs @@ -1528,19 +1528,20 @@ impl Background { ) { let mut guarded = self.guarded.lock().await; - guarded.runtimes.retain(|_, rt| rt.strong_count() > 0); - - // Try to find an existing identical runtime. + // Try to find an existing runtime identical to the one that has just been downloaded. + // This loop is `O(n)`, but given that we expect this list to very small (at most 1 or + // 2 elements), this is not a problem. let existing_runtime = guarded .runtimes .iter() .filter_map(|(_, rt)| rt.upgrade()) .find(|rt| rt.runtime_code == storage_code && rt.heap_pages == storage_heap_pages); + // If no identical runtime was found, try compiling the runtime. + // TODO: use a let-else construct here once stable let runtime = if let Some(existing_runtime) = existing_runtime { existing_runtime } else { - // No identical runtime was found. Try compiling the new runtime. let runtime = SuccessfulRuntime::from_storage(&storage_code, &storage_heap_pages).await; match &runtime { Ok(runtime) => { @@ -1572,6 +1573,7 @@ impl Background { runtime }; + // Insert the runtime into the tree. match &mut guarded.tree { GuardedInner::FinalizedBlockRuntimeKnown { tree, .. } => { tree.async_op_finished(async_op_id, runtime); @@ -1598,6 +1600,7 @@ impl Background { user_data: new_finalized, best_block_index, pruned_blocks, + former_finalized_async_op_user_data: former_finalized_runtime, .. }) => { *finalized_block = new_finalized; @@ -1610,6 +1613,13 @@ impl Background { HashDisplay(&finalized_block.hash), HashDisplay(&best_block_hash) ); + // The finalization might cause some runtimes in the list of runtimes + // to have become unused. Clean them up. + drop(former_finalized_runtime); + guarded + .runtimes + .retain(|_, runtime| runtime.strong_count() > 0); + let all_blocks_notif = Notification::Finalized { best_block_hash, hash: finalized_block.hash, @@ -1965,7 +1975,7 @@ impl Background { // Clean up unused runtimes to free up resources. guarded .runtimes - .retain(|_, runtime| runtime.strong_count() == 0); + .retain(|_, runtime| runtime.strong_count() > 0); } } diff --git a/bin/wasm-node/CHANGELOG.md b/bin/wasm-node/CHANGELOG.md index 8e8651d9f2..75b7eb0013 100644 --- a/bin/wasm-node/CHANGELOG.md +++ b/bin/wasm-node/CHANGELOG.md @@ -6,6 +6,10 @@ - Removed the `version` field of the struct returned by the `rpc_methods` function. This is technically a breaking change, but it has been introduced in a minor version bump because it is very insubstantial. ([#2756](https://github.com/paritytech/smoldot/pull/2756)) +### Fixed + +- Fix old runtimes not being cleaned up properly and runtimes being downloaded multiple times after an on-chain runtime upgrade. + ## 0.6.34 - 2022-09-20 ### Added From 002235ec172f4e92571d28ab7845e6c3f1f340d5 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 26 Sep 2022 10:29:08 +0200 Subject: [PATCH 2/2] CHANGELOG --- bin/wasm-node/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/wasm-node/CHANGELOG.md b/bin/wasm-node/CHANGELOG.md index 75b7eb0013..7501362577 100644 --- a/bin/wasm-node/CHANGELOG.md +++ b/bin/wasm-node/CHANGELOG.md @@ -8,7 +8,7 @@ ### Fixed -- Fix old runtimes not being cleaned up properly and runtimes being downloaded multiple times after an on-chain runtime upgrade. +- Fix old runtimes not being cleaned up properly and runtimes being downloaded multiple times after an on-chain runtime upgrade. ([#2781](https://github.com/paritytech/smoldot/pull/2781)) ## 0.6.34 - 2022-09-20