From 75301f01dfa7dd73f4949ed65484442a8cf70cf1 Mon Sep 17 00:00:00 2001 From: ELA CITY Date: Fri, 16 Oct 2026 17:12:10 +0000 Subject: [PATCH 1/4] perf(compute): keep bounded Component compilation off async workers Review package extracted from preserved candidate b0fa431d. The original eighteen commits remain on fix/runtime-performance-080. Validation of this recomposed head is recorded separately from historical candidate evidence. --- .../src/providers/component.rs | 214 +++++++++++++++++- 1 file changed, 202 insertions(+), 12 deletions(-) diff --git a/elastos/crates/elastos-compute/src/providers/component.rs b/elastos/crates/elastos-compute/src/providers/component.rs index af5874525..247addcbf 100644 --- a/elastos/crates/elastos-compute/src/providers/component.rs +++ b/elastos/crates/elastos-compute/src/providers/component.rs @@ -4,7 +4,7 @@ use async_trait::async_trait; use std::collections::HashMap; use std::path::Path; use std::sync::Arc; -use tokio::sync::RwLock; +use tokio::sync::{RwLock, Semaphore}; use wasmtime::component::{bindgen, Component, HasSelf, Linker}; use wasmtime::{Config, Engine, Store, StoreLimits, StoreLimitsBuilder}; @@ -49,6 +49,7 @@ struct ComponentState { /// hostcall configured by Runtime. pub struct ComponentProvider { instances: Arc>>, + compile_slot: Arc, bus_hostcall: std::sync::RwLock>, bridge_principals: Arc>>>, } @@ -57,6 +58,9 @@ impl ComponentProvider { pub fn new() -> Self { Self { instances: Arc::new(RwLock::new(HashMap::new())), + // Wasmtime parallelizes each compile internally. Queue loads so + // bursts do not create competing compiler jobs. + compile_slot: Arc::new(Semaphore::new(1)), bus_hostcall: std::sync::RwLock::new(None), bridge_principals: Arc::new(RwLock::new(HashMap::new())), } @@ -154,17 +158,36 @@ impl ComputeProvider for ComponentProvider { } let component_path = path.join(&manifest.entrypoint); - if !component_path.exists() { - return Err(ElastosError::CapsuleNotFound(format!( - "component file not found: {}", - component_path.display() - ))); - } - - let engine = Self::engine()?; - let component = Component::from_file(&engine, &component_path).map_err(|err| { - ElastosError::Compute(format!("Failed to compile component capsule: {err}")) - })?; + // File reads and Wasmtime compilation can take substantial time. Keep + // unrelated Runtime work responsive while preparing the activation. + let compile_permit = self + .compile_slot + .clone() + .acquire_owned() + .await + .map_err(|_| ElastosError::Compute("Component compiler is unavailable".into()))?; + let (engine, component) = + tokio::task::spawn_blocking(move || -> Result<(Engine, Component)> { + // A cancelled caller can leave blocking work in progress. The + // work retains its slot until it actually finishes. + let _compile_permit = compile_permit; + if !component_path.exists() { + return Err(ElastosError::CapsuleNotFound(format!( + "component file not found: {}", + component_path.display() + ))); + } + + let engine = Self::engine()?; + let component = Component::from_file(&engine, &component_path).map_err(|err| { + ElastosError::Compute(format!("Failed to compile component capsule: {err}")) + })?; + Ok((engine, component)) + }) + .await + .map_err(|err| { + ElastosError::Compute(format!("Component load task join error: {err}")) + })??; let id = CapsuleId::new(format!("component-{}", uuid::Uuid::new_v4())); self.instances.write().await.insert( @@ -419,7 +442,174 @@ impl elastos::bus::providers::Host for ComponentState { #[cfg(test)] mod tests { use super::*; + use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Mutex; + use std::time::{Duration, Instant}; + + fn component_fixture() -> (std::path::PathBuf, CapsuleManifest) { + let path = + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/components/bus-v1"); + let manifest = + serde_json::from_slice(&std::fs::read(path.join("capsule.json")).unwrap()).unwrap(); + (path, manifest) + } + + #[test] + fn component_load_keeps_async_work_responsive() { + let runtime = tokio::runtime::Builder::new_current_thread() + .max_blocking_threads(1) + .enable_all() + .build() + .unwrap(); + let (release, waiting) = std::sync::mpsc::channel(); + let blocker = runtime.spawn_blocking(move || { + let _ = waiting.recv(); + }); + let provider = Arc::new(ComponentProvider::new()); + let (path, manifest) = component_fixture(); + + runtime.block_on(async { + let (started, observing) = tokio::sync::oneshot::channel(); + let loading_provider = provider.clone(); + let loading = tokio::spawn(async move { + started.send(()).unwrap(); + loading_provider.load(&path, manifest).await + }); + observing.await.unwrap(); + tokio::task::yield_now().await; + let yielded_before_loading_finished = !loading.is_finished(); + + // Always release the worker before checking the result, including + // when a regression makes load run on the async executor. + release.send(()).unwrap(); + let handle = loading.await.unwrap().unwrap(); + blocker.await.unwrap(); + assert_eq!( + provider.status(&handle).await.unwrap(), + CapsuleStatus::Loading + ); + provider.stop(&handle).await.unwrap(); + assert!(matches!( + provider.status(&handle).await, + Err(ElastosError::CapsuleNotFound(_)) + )); + assert!( + yielded_before_loading_finished, + "component loading prevented unrelated async work from running" + ); + }); + } + + #[tokio::test(flavor = "current_thread")] + async fn component_load_reports_heartbeat_delay() { + let provider = ComponentProvider::new(); + let (path, manifest) = component_fixture(); + let completed = AtomicBool::new(false); + let started = Instant::now(); + let loading = async { + let result = provider.load(&path, manifest).await; + completed.store(true, Ordering::Relaxed); + result + }; + let heartbeat = async { + let mut previous = started; + let mut max_delay = Duration::ZERO; + let mut ticks = 0; + loop { + let now = Instant::now(); + max_delay = max_delay.max(now.duration_since(previous)); + if completed.load(Ordering::Relaxed) { + break; + } + ticks += 1; + previous = now; + tokio::time::sleep(Duration::from_millis(1)).await; + } + (ticks, max_delay) + }; + let (handle, (ticks, max_delay)) = tokio::join!(biased; loading, heartbeat); + println!( + "component load: elapsed={:?}, heartbeat_ticks={}, max_heartbeat_delay={:?}", + started.elapsed(), + ticks, + max_delay + ); + provider.stop(&handle.unwrap()).await.unwrap(); + } + + #[test] + fn component_load_retains_compile_slot_after_cancellation() { + let runtime = tokio::runtime::Builder::new_current_thread() + .max_blocking_threads(1) + .enable_all() + .build() + .unwrap(); + let (release, waiting) = std::sync::mpsc::channel(); + let blocker = runtime.spawn_blocking(move || { + let _ = waiting.recv(); + }); + let provider = Arc::new(ComponentProvider::new()); + let (path, manifest) = component_fixture(); + + runtime.block_on(async { + let (started, observing) = tokio::sync::oneshot::channel(); + let loading_provider = provider.clone(); + let first_path = path.clone(); + let first_manifest = manifest.clone(); + let loading = tokio::spawn(async move { + started.send(()).unwrap(); + loading_provider.load(&first_path, first_manifest).await + }); + observing.await.unwrap(); + tokio::task::yield_now().await; + let slot_held_during_load = provider.compile_slot.available_permits() == 0; + loading.abort(); + let cancelled = loading.await.unwrap_err().is_cancelled(); + let slot_held_after_cancellation = provider.compile_slot.available_permits() == 0; + + let (started, observing) = tokio::sync::oneshot::channel(); + let loading_provider = provider.clone(); + let next_load = tokio::spawn(async move { + started.send(()).unwrap(); + loading_provider.load(&path, manifest).await + }); + observing.await.unwrap(); + tokio::task::yield_now().await; + let next_load_waited = !next_load.is_finished(); + + release.send(()).unwrap(); + blocker.await.unwrap(); + let handle = next_load.await.unwrap().unwrap(); + provider.stop(&handle).await.unwrap(); + + assert!(slot_held_during_load); + assert!(cancelled); + assert!(slot_held_after_cancellation); + assert!(next_load_waited); + assert_eq!(provider.compile_slot.available_permits(), 1); + assert!(provider.instances.read().await.is_empty()); + }); + } + + #[tokio::test] + async fn component_load_preserves_artifact_errors() { + let provider = ComponentProvider::new(); + let (_, manifest) = component_fixture(); + let directory = tempfile::tempdir().unwrap(); + assert!(matches!( + provider.load(directory.path(), manifest.clone()).await, + Err(ElastosError::CapsuleNotFound(message)) + if message.starts_with("component file not found: ") + )); + std::fs::write(directory.path().join(&manifest.entrypoint), b"invalid wasm").unwrap(); + assert!(matches!( + provider.load(directory.path(), manifest).await, + Err(ElastosError::Compute(message)) + if message.starts_with("Failed to compile component capsule: ") + )); + assert!(provider.instances.read().await.is_empty()); + assert_eq!(provider.compile_slot.available_permits(), 1); + } fn json_line(line: &str) -> serde_json::Value { serde_json::from_str(line).expect("captured bridge line must be JSON") From 2defb2fb953942a87a94149c663aad91b2f34e93 Mon Sep 17 00:00:00 2001 From: SashaMIT Date: Sat, 17 Oct 2026 14:07:14 +0000 Subject: [PATCH 2/4] Bound the wait for a Component compilation slot Co-authored-by: Codex --- .../src/providers/component.rs | 78 +++++++++---------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/elastos/crates/elastos-compute/src/providers/component.rs b/elastos/crates/elastos-compute/src/providers/component.rs index 247addcbf..bed4a8fd6 100644 --- a/elastos/crates/elastos-compute/src/providers/component.rs +++ b/elastos/crates/elastos-compute/src/providers/component.rs @@ -4,6 +4,7 @@ use async_trait::async_trait; use std::collections::HashMap; use std::path::Path; use std::sync::Arc; +use std::time::Duration; use tokio::sync::{RwLock, Semaphore}; use wasmtime::component::{bindgen, Component, HasSelf, Linker}; use wasmtime::{Config, Engine, Store, StoreLimits, StoreLimitsBuilder}; @@ -27,6 +28,7 @@ bindgen!({ const COMPONENT_FUEL: u64 = 100_000_000; const COMPONENT_MEMORY_SIZE: usize = 128 * 1024 * 1024; +const COMPONENT_COMPILE_WAIT_TIMEOUT: Duration = Duration::from_secs(30); struct ComponentActivation { engine: Engine, @@ -160,12 +162,15 @@ impl ComputeProvider for ComponentProvider { let component_path = path.join(&manifest.entrypoint); // File reads and Wasmtime compilation can take substantial time. Keep // unrelated Runtime work responsive while preparing the activation. - let compile_permit = self - .compile_slot - .clone() - .acquire_owned() - .await - .map_err(|_| ElastosError::Compute("Component compiler is unavailable".into()))?; + // Bound the wait for capacity; an admitted blocking job keeps its slot + // until the actual compile finishes, even if its caller is cancelled. + let compile_permit = tokio::time::timeout( + COMPONENT_COMPILE_WAIT_TIMEOUT, + self.compile_slot.clone().acquire_owned(), + ) + .await + .map_err(|_| ElastosError::Compute("Timed out waiting for the component compiler".into()))? + .map_err(|_| ElastosError::Compute("Component compiler is unavailable".into()))?; let (engine, component) = tokio::task::spawn_blocking(move || -> Result<(Engine, Component)> { // A cancelled caller can leave blocking work in progress. The @@ -442,9 +447,7 @@ impl elastos::bus::providers::Host for ComponentState { #[cfg(test)] mod tests { use super::*; - use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Mutex; - use std::time::{Duration, Instant}; fn component_fixture() -> (std::path::PathBuf, CapsuleManifest) { let path = @@ -500,41 +503,34 @@ mod tests { }); } - #[tokio::test(flavor = "current_thread")] - async fn component_load_reports_heartbeat_delay() { - let provider = ComponentProvider::new(); + #[tokio::test(start_paused = true)] + async fn component_load_times_out_waiting_for_compile_slot() { + let provider = Arc::new(ComponentProvider::new()); + let held_permit = provider.compile_slot.clone().acquire_owned().await.unwrap(); let (path, manifest) = component_fixture(); - let completed = AtomicBool::new(false); - let started = Instant::now(); - let loading = async { - let result = provider.load(&path, manifest).await; - completed.store(true, Ordering::Relaxed); - result - }; - let heartbeat = async { - let mut previous = started; - let mut max_delay = Duration::ZERO; - let mut ticks = 0; - loop { - let now = Instant::now(); - max_delay = max_delay.max(now.duration_since(previous)); - if completed.load(Ordering::Relaxed) { - break; - } - ticks += 1; - previous = now; - tokio::time::sleep(Duration::from_millis(1)).await; - } - (ticks, max_delay) - }; - let (handle, (ticks, max_delay)) = tokio::join!(biased; loading, heartbeat); - println!( - "component load: elapsed={:?}, heartbeat_ticks={}, max_heartbeat_delay={:?}", - started.elapsed(), - ticks, - max_delay + let loading_provider = provider.clone(); + let mut loading = tokio::spawn(async move { loading_provider.load(&path, manifest).await }); + + // The outer deadline makes a missing queue limit fail instead of hang. + // Paused Tokio time avoids relying on compilation or host speed. + let result = tokio::time::timeout( + COMPONENT_COMPILE_WAIT_TIMEOUT + Duration::from_secs(1), + &mut loading, + ) + .await; + if result.is_err() { + loading.abort(); + let _ = loading.await; + } + drop(held_permit); + + assert!( + matches!(result, Ok(Ok(Err(ElastosError::Compute(message)))) + if message == "Timed out waiting for the component compiler"), + "a queued component load must return its own timeout before the outer deadline" ); - provider.stop(&handle.unwrap()).await.unwrap(); + assert!(provider.instances.read().await.is_empty()); + assert_eq!(provider.compile_slot.available_permits(), 1); } #[test] From ea5dafee66139e65176e604406f748a57624e097 Mon Sep 17 00:00:00 2001 From: SashaMIT Date: Sun, 18 Oct 2026 00:56:20 +0000 Subject: [PATCH 3/4] Reuse the platform-neutral Home link test fixture Apply the test-only System fixture from 619c0961964aa2a5da938af5cba622349edb1382 to the refreshed develop base. Preserve the existing authority assertions and production behavior. Co-authored-by: Codex --- .../src/api/gateway_tests/home_system.rs | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/elastos/crates/elastos-server/src/api/gateway_tests/home_system.rs b/elastos/crates/elastos-server/src/api/gateway_tests/home_system.rs index a30bded9c..02d08fc68 100644 --- a/elastos/crates/elastos-server/src/api/gateway_tests/home_system.rs +++ b/elastos/crates/elastos-server/src/api/gateway_tests/home_system.rs @@ -11945,9 +11945,10 @@ async fn home_external_link_names_the_launched_app_and_the_exact_address() { }) }; - let browser_token = launched(BROWSER_CAPSULE_ID); + // System is launchable on every host; Browser availability is platform-specific. + let system_token = launched(SYSTEM_CAPSULE_ID); for (app_token, app_id, app_title) in [ - (browser_token.clone(), BROWSER_CAPSULE_ID, "Browser"), + (system_token.clone(), SYSTEM_CAPSULE_ID, "System"), ( launched(HOME_CLI_CAPSULE_ID_FOR_TEST), "home-cli", @@ -11979,13 +11980,13 @@ async fn home_external_link_names_the_launched_app_and_the_exact_address() { "foreign origin", "https://evil.example", home_token.clone(), - browser_token.clone(), + system_token.clone(), ), ( "an app frame calling directly", "null", home_token.clone(), - browser_token.clone(), + system_token.clone(), ), ( "app token from another Home session", @@ -11993,8 +11994,8 @@ async fn home_external_link_names_the_launched_app_and_the_exact_address() { home_token.clone(), issue_home_projection_launch_token_with_context( dir.path(), - BROWSER_CAPSULE_ID, - BROWSER_CAPSULE_ID, + SYSTEM_CAPSULE_ID, + SYSTEM_CAPSULE_ID, &other_session, ) .unwrap(), @@ -12004,13 +12005,13 @@ async fn home_external_link_names_the_launched_app_and_the_exact_address() { "http://localhost:61180", issue_home_launch_token_with_context(dir.path(), HOME_CAPSULE_ID, &other_session) .unwrap(), - browser_token.clone(), + system_token.clone(), ), ( "app token Home did not mint", "http://localhost:61180", home_token.clone(), - issue_home_launch_token_with_context(dir.path(), BROWSER_CAPSULE_ID, &context).unwrap(), + issue_home_launch_token_with_context(dir.path(), SYSTEM_CAPSULE_ID, &context).unwrap(), ), ( "Home GUI is not a link sender", @@ -12037,7 +12038,7 @@ async fn home_external_link_names_the_launched_app_and_the_exact_address() { issue_home_projection_launch_token_with_context( dir.path(), "assistant", - BROWSER_CAPSULE_ID, + SYSTEM_CAPSULE_ID, &context, ) .unwrap(), @@ -12061,7 +12062,7 @@ async fn home_external_link_names_the_launched_app_and_the_exact_address() { } let expired = - issue_expired_home_launch_token_with_context(dir.path(), BROWSER_CAPSULE_ID, &context) + issue_expired_home_launch_token_with_context(dir.path(), SYSTEM_CAPSULE_ID, &context) .unwrap(); let (status, body) = home_external_link_raw( &app, @@ -12088,7 +12089,7 @@ async fn home_external_link_names_the_launched_app_and_the_exact_address() { &app, "http://localhost:61180", &home_token, - request(&browser_token, url), + request(&system_token, url), ) .await; assert_eq!(status, StatusCode::BAD_REQUEST, "{url:?}: {body}"); @@ -12099,13 +12100,13 @@ async fn home_external_link_names_the_launched_app_and_the_exact_address() { &home_token, json!({ "schema": "elastos.home.external-link.request/v2", - "app_token": browser_token, + "app_token": system_token, "url": "https://example.com/", }), ) .await; assert_eq!(status, StatusCode::BAD_REQUEST); - let mut extra = request(&browser_token, "https://example.com/"); + let mut extra = request(&system_token, "https://example.com/"); extra["app_id"] = json!("wallet"); let (status, _) = home_external_link_raw(&app, "http://localhost:61180", &home_token, extra).await; From 6610d115329e3df08d5ab2a511e1b018c465f018 Mon Sep 17 00:00:00 2001 From: SashaMIT Date: Sun, 18 Oct 2026 14:16:36 +0000 Subject: [PATCH 4/4] Prove compilation ownership survives caller cancellation Control admission and the blocked worker, then require a subsequent load to reach its queue timeout while the original compile retains the slot. Co-authored-by: Codex --- .../src/providers/component.rs | 61 +++++++++++++------ 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/elastos/crates/elastos-compute/src/providers/component.rs b/elastos/crates/elastos-compute/src/providers/component.rs index bed4a8fd6..16b22e709 100644 --- a/elastos/crates/elastos-compute/src/providers/component.rs +++ b/elastos/crates/elastos-compute/src/providers/component.rs @@ -447,7 +447,9 @@ impl elastos::bus::providers::Host for ComponentState { #[cfg(test)] mod tests { use super::*; + use std::future::Future; use std::sync::Mutex; + use std::task::Poll; fn component_fixture() -> (std::path::PathBuf, CapsuleManifest) { let path = @@ -538,51 +540,74 @@ mod tests { let runtime = tokio::runtime::Builder::new_current_thread() .max_blocking_threads(1) .enable_all() + .start_paused(true) .build() .unwrap(); let (release, waiting) = std::sync::mpsc::channel(); + let (blocked, observing_blocker) = tokio::sync::oneshot::channel(); let blocker = runtime.spawn_blocking(move || { + blocked.send(()).unwrap(); let _ = waiting.recv(); }); let provider = Arc::new(ComponentProvider::new()); let (path, manifest) = component_fixture(); runtime.block_on(async { + observing_blocker.await.unwrap(); let (started, observing) = tokio::sync::oneshot::channel(); let loading_provider = provider.clone(); let first_path = path.clone(); let first_manifest = manifest.clone(); let loading = tokio::spawn(async move { + let mut load = loading_provider.load(&first_path, first_manifest); + // Poll through admission before acknowledging the caller. The + // sole blocking worker stays gated until both waits finish. + let initial = std::future::poll_fn(|cx| Poll::Ready(load.as_mut().poll(cx))).await; started.send(()).unwrap(); - loading_provider.load(&first_path, first_manifest).await + match initial { + Poll::Ready(result) => result, + Poll::Pending => load.await, + } }); observing.await.unwrap(); - tokio::task::yield_now().await; - let slot_held_during_load = provider.compile_slot.available_permits() == 0; loading.abort(); let cancelled = loading.await.unwrap_err().is_cancelled(); - let slot_held_after_cancellation = provider.compile_slot.available_permits() == 0; - let (started, observing) = tokio::sync::oneshot::channel(); - let loading_provider = provider.clone(); - let next_load = tokio::spawn(async move { - started.send(()).unwrap(); - loading_provider.load(&path, manifest).await - }); - observing.await.unwrap(); - tokio::task::yield_now().await; - let next_load_waited = !next_load.is_finished(); + let result = { + let next_load = tokio::time::timeout( + COMPONENT_COMPILE_WAIT_TIMEOUT + Duration::from_secs(1), + provider.load(&path, manifest.clone()), + ); + tokio::pin!(next_load); + // Register the queue wait, then advance virtual time while the + // cancelled caller's compile still owns its slot. Polling the + // load directly lets its queue timeout win over the outer guard. + match std::future::poll_fn(|cx| Poll::Ready(next_load.as_mut().poll(cx))).await { + Poll::Ready(result) => result, + Poll::Pending => { + tokio::time::advance( + COMPONENT_COMPILE_WAIT_TIMEOUT + Duration::from_secs(1), + ) + .await; + next_load.await + } + } + }; + // Release and drain the blocking work before asserting, including + // when early slot release admitted the second load to the worker. release.send(()).unwrap(); blocker.await.unwrap(); - let handle = next_load.await.unwrap().unwrap(); + tokio::time::resume(); + let handle = provider.load(&path, manifest).await.unwrap(); provider.stop(&handle).await.unwrap(); - assert!(slot_held_during_load); assert!(cancelled); - assert!(slot_held_after_cancellation); - assert!(next_load_waited); - assert_eq!(provider.compile_slot.available_permits(), 1); + assert!( + matches!(result, Ok(Err(ElastosError::Compute(message))) + if message == "Timed out waiting for the component compiler"), + "cancelling a load must keep later loads queued until its blocking compile finishes" + ); assert!(provider.instances.read().await.is_empty()); }); }