From ed49c283a250ad60f098f0b3d202dac63aac413b Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 27 Oct 2026 12:24:19 +0300 Subject: [PATCH] voelin-platform: zbus on async-io alone in every binary (notify-rust `z`), and a test for the order of ashpd's callers ashpd 0.13.13 keeps one session connection per process (a static in its proxy.rs), and zbus 5.19 picks that connection's executor once, from Handle::try_current() on the thread that builds it, when both its `tokio` and `async-io` features are compiled in. The first ashpd caller therefore decided the executor of every later portal call: a tokio caller first, then a caller with no runtime (a GPUI foreground task), panics with "there is no reactor running" (get_property("version") sets up zbus's property cache, which calls tokio::task::spawn). The merged binary was safe only because GPUI's colour-scheme call builds the connection before the engine's portal calls. voelin-platform's notify-rust asks for `z` (async-io) in place of `z-with-tokio`, and the stand-in portal's zbus dev-dependency for `async-io` instead of `tokio`: a test binary with both features shows the panic even when the binaries do not have it. `cargo tree -e features -i zbus` now names `tokio` nowhere; Cargo.lock loses zbus's tokio edge and tokio's tracing edge (THIRD_PARTY_NOTICES.md is unchanged). tests/proxy.rs gets the gate's probe shape on a private dbus-run-session bus (skipped with a printed reason where none can be made): a tokio caller first, then a caller with no runtime. It fails with the panic on the tree before this change, and still did with only notify-rust moved, which is why the dev-dependency moved too. The four records that described the move wrongly are corrected: docs/design/gpui-spikes.md "Dependencies", the comment of PortalCapture::start, voelin-media's Cargo.toml and CHANGELOG.md (whose Slint-only sentence goes: the Slint app's zbus connections move to async-io threads too). Co-Authored-By: Claude Sonnet 5.5 --- CHANGELOG.md | 4 +- Cargo.lock | 2 - crates/voelin-media/Cargo.toml | 6 +- crates/voelin-media/src/capture/portal.rs | 6 +- crates/voelin-platform/Cargo.toml | 9 ++- crates/voelin-platform/tests/proxy.rs | 90 ++++++++++++++++------- docs/design/gpui-spikes.md | 30 ++++++-- 7 files changed, 102 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6715b9c..4145942 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -91,7 +91,9 @@ User-visible changes of the apps and the `tsgw` gateway. The format follows talk shortcut, the file chooser and the proxy settings) talk to D-Bus through ashpd on `async-io` instead of `tokio`. Nothing changes on screen; the GPUI front end, which is being built, enables ashpd's `async-io` and ashpd cannot - have both. Inside the engine's tokio runtime zbus still runs on it. + have both. Every D-Bus connection of the apps, the desktop notifications' + included, now runs on an I/O thread of its own, whichever part of the app + asks first. - The build needs Rust 1.95 (`rust-toolchain.toml` and the workspace's `rust-version`; 1.95.0 was used to check it), up from 1.94: the GPUI front end, which is being built, depends on a library that calls a function diff --git a/Cargo.lock b/Cargo.lock index eb020f5..64b60f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11434,7 +11434,6 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", - "tracing", "windows-sys 0.61.2", ] @@ -14526,7 +14525,6 @@ dependencies = [ "rustix 1.1.5", "serde", "serde_repr", - "tokio", "tracing", "uds_windows", "uuid", diff --git a/crates/voelin-media/Cargo.toml b/crates/voelin-media/Cargo.toml index 4c51769..cf5ad7a 100644 --- a/crates/voelin-media/Cargo.toml +++ b/crates/voelin-media/Cargo.toml @@ -71,8 +71,10 @@ yuv = "0.8.19" [target.'cfg(target_os = "linux")'.dependencies] # `async-io`, not `tokio`: GPUI's Linux backend enables ashpd's `async-io`, ashpd # refuses both at once, and a front end holds the portals and GPUI in one binary. -# zbus then runs a connection on its own I/O thread, or on tokio when it is made -# inside a tokio runtime and another crate turns zbus's `tokio` feature on. +# zbus then runs every connection on an I/O thread of its own, whichever thread +# builds it: no crate of the workspace turns zbus's `tokio` feature on (with it +# zbus would pick tokio for a connection made inside a tokio runtime, and ashpd +# shares one connection per process with every later caller). ashpd = { version = "0.13.13", default-features = false, features = ["camera", "screencast", "async-io"], optional = true } memmap2 = { version = "0.9.11", optional = true } pipewire = { version = "0.10.1", optional = true } diff --git a/crates/voelin-media/src/capture/portal.rs b/crates/voelin-media/src/capture/portal.rs index 32bc57e..d76a104 100644 --- a/crates/voelin-media/src/capture/portal.rs +++ b/crates/voelin-media/src/capture/portal.rs @@ -181,9 +181,9 @@ impl ScreenCapture for PortalCapture { } /// Asks the user through the portal dialog (unless a restore token - /// applies). Any executor will do: ashpd runs on zbus's `async-io`, whose - /// connection has an I/O thread of its own (inside a tokio runtime, with - /// zbus's `tokio` feature on as well, it runs on tokio instead). + /// applies). Any executor will do: ashpd runs on zbus's `async-io` in every + /// binary of the workspace (no crate turns zbus's `tokio` feature on), whose + /// connection has an I/O thread of its own, whichever thread builds it. fn start( &mut self, source: &SourceId, diff --git a/crates/voelin-platform/Cargo.toml b/crates/voelin-platform/Cargo.toml index 0643cff..58a0d1f 100644 --- a/crates/voelin-platform/Cargo.toml +++ b/crates/voelin-platform/Cargo.toml @@ -28,7 +28,7 @@ tracing = "0.1" # ashpd refuses both features at once. ashpd = { version = "0.13", default-features = false, features = ["async-io", "global_shortcuts", "file_chooser", "proxy_resolver"], optional = true } futures-util = { version = "0.3", optional = true } -notify-rust = { version = "4.18", default-features = false, features = ["z-with-tokio"] } +notify-rust = { version = "4.18", default-features = false, features = ["z"] } x11rb = { version = "0.13", features = ["xinput"], optional = true } [target.'cfg(windows)'.dependencies] @@ -48,8 +48,11 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread", "time"] } [target.'cfg(all(unix, not(target_os = "macos")))'.dev-dependencies] x11rb = { version = "0.13", features = ["xinput", "xtest"] } # A stand-in desktop portal on a private session bus (tests/proxy.rs); the -# zbus ashpd uses. -zbus = { version = "5", default-features = false, features = ["tokio"] } +# zbus ashpd uses, on `async-io` like every other zbus in the workspace: a +# `tokio` here would put the executor choice of ashpd's connection (zbus picks +# tokio when a tokio runtime is current where it builds the connection) into +# the tests alone, and tests/proxy.rs checks that the choice does not matter. +zbus = { version = "5", default-features = false, features = ["async-io"] } [lints] workspace = true diff --git a/crates/voelin-platform/tests/proxy.rs b/crates/voelin-platform/tests/proxy.rs index 59b2948..998ca35 100644 --- a/crates/voelin-platform/tests/proxy.rs +++ b/crates/voelin-platform/tests/proxy.rs @@ -6,9 +6,10 @@ //! or a bus daemon for it, the test says so and checks nothing. Its own test //! binary: answers are kept process-wide. //! -//! The second test calls ashpd from a thread with no tokio runtime, as GPUI's -//! executor would: ashpd runs on zbus's `async-io` there (docs/design/gpui-spikes.md, -//! "Dependencies"). +//! The second and third tests call ashpd from a thread with no tokio runtime, +//! as GPUI's executor would: ashpd runs on zbus's `async-io`, whichever thread +//! built its connection (the third lets a tokio caller build it first; +//! docs/design/gpui-spikes.md, "Dependencies"). #![cfg(all(unix, not(target_os = "macos"), feature = "portal"))] @@ -158,6 +159,38 @@ fn block_on(future: F) -> F::Output { } } +/// The stand-in portal on the bus of its own, served from the calling task. +async fn stand_in_portal(lookups: &Arc) -> zbus::Connection { + zbus::connection::Builder::session() + .unwrap() + .name("org.freedesktop.portal.Desktop") + .unwrap() + .serve_at("/org/freedesktop/portal/desktop", Resolver { lookups: lookups.clone() }) + .unwrap() + .build() + .await + .unwrap() +} + +/// A lookup through ashpd, from the calling task. +async fn lookup() -> Vec { + let resolver = ashpd::desktop::proxy_resolver::ProxyResolver::new().await.unwrap(); + let uri = ashpd::Uri::parse("https://capped.example/").unwrap(); + resolver.lookup(&uri).await.unwrap() +} + +/// A lookup through ashpd from a thread of its own with no runtime, blocking +/// the caller's task for it (the pool's threads, `spawn_blocking`, have the +/// runtime in reach). The stand-in portal runs on the test's runtime: keep it +/// free while waiting. +async fn lookup_without_a_runtime() -> Vec { + let caller = std::thread::spawn(|| { + assert!(tokio::runtime::Handle::try_current().is_err(), "this thread has no runtime"); + block_on(lookup()).iter().map(|uri| uri.as_str().to_owned()).collect::>() + }); + tokio::task::spawn_blocking(move || caller.join().unwrap()).await.unwrap() +} + /// ashpd with `async-io`: a caller outside any tokio runtime gets its answer, /// the connection's I/O running on zbus's own thread. #[tokio::test(flavor = "multi_thread")] @@ -168,28 +201,33 @@ async fn the_portal_answers_a_caller_without_a_runtime() { return; } let lookups = Arc::new(AtomicU32::new(0)); - let _portal = zbus::connection::Builder::session() - .unwrap() - .name("org.freedesktop.portal.Desktop") - .unwrap() - .serve_at("/org/freedesktop/portal/desktop", Resolver { lookups: lookups.clone() }) - .unwrap() - .build() - .await - .unwrap(); - // A thread of its own: the pool's threads (`spawn_blocking`) have the - // runtime in reach. - let caller = std::thread::spawn(|| { - assert!(tokio::runtime::Handle::try_current().is_err(), "this thread has no runtime"); - block_on(async { - let resolver = ashpd::desktop::proxy_resolver::ProxyResolver::new().await.unwrap(); - let uri = ashpd::Uri::parse("https://capped.example/").unwrap(); - resolver.lookup(&uri).await.unwrap() - }) - }); - // The stand-in portal runs on this runtime: keep it free while waiting. - let answer = tokio::task::spawn_blocking(move || caller.join().unwrap()).await.unwrap(); - let answer: Vec<&str> = answer.iter().map(|uri| uri.as_str()).collect(); - assert_eq!(answer, ["socks://127.0.0.1:2080"]); + let _portal = stand_in_portal(&lookups).await; + assert_eq!(lookup_without_a_runtime().await, ["socks://127.0.0.1:2080"]); assert_eq!(lookups.load(Ordering::SeqCst), 1); } + +/// ashpd keeps one session connection for the process, and zbus picks the +/// executor of a connection once, when it builds it: tokio if a tokio runtime +/// is current on that thread and zbus has its `tokio` feature on, its own +/// `async-io` thread otherwise. If the engine's runtime made the connection +/// first, a later caller with no runtime (a GPUI foreground task) would find a +/// tokio connection with no reactor to run on and panic ("there is no reactor +/// running" in zbus's property cache). With zbus on `async-io` alone in every +/// binary (notify-rust without `z-with-tokio`, no zbus `tokio` here either) +/// the order does not matter. +#[tokio::test(flavor = "multi_thread")] +async fn the_portal_answers_a_caller_without_a_runtime_after_a_tokio_caller() { + const TEST: &str = "the_portal_answers_a_caller_without_a_runtime_after_a_tokio_caller"; + if std::env::var_os(ON_ITS_OWN_BUS).is_none() { + on_a_bus_of_its_own(TEST); + return; + } + let lookups = Arc::new(AtomicU32::new(0)); + let _portal = stand_in_portal(&lookups).await; + // The tokio caller first: it builds the process's session connection. + let answer: Vec<_> = lookup().await.iter().map(|uri| uri.as_str().to_owned()).collect(); + assert_eq!(answer, ["socks://127.0.0.1:2080"]); + // Then the caller with no runtime, on the same connection. + assert_eq!(lookup_without_a_runtime().await, ["socks://127.0.0.1:2080"]); + assert_eq!(lookups.load(Ordering::SeqCst), 2); +} diff --git a/docs/design/gpui-spikes.md b/docs/design/gpui-spikes.md index 7d0b53d..886df30 100644 --- a/docs/design/gpui-spikes.md +++ b/docs/design/gpui-spikes.md @@ -468,14 +468,28 @@ the raw source's `for n in 0u64..` in `spikes/gpui/video/src/main.rs`, now and `voelin-gpui` holds both (it depends on `voelin-core` with `media-desktop`, which brings PipeWire, and on `voelin-platform` with its portal feature). What zbus 5.19 does with the features (read in its - source): with `async-io` and `tokio` both compiled in it picks per - connection, tokio when a tokio runtime is current on the calling thread and - `async-io` with an I/O thread of its own otherwise. The workspace has both - on already (notify-rust asks for `tokio`, others for the defaults), so the - engine's portal calls inside its runtime still run on tokio and a caller - with no runtime (a GPUI foreground task) gets the thread; a test in - voelin-platform (`tests/proxy.rs`) calls ashpd from a thread with no tokio - runtime and gets the answer. + source): with `async-io` and `tokio` both compiled in it picks an executor + once per connection, tokio when a tokio runtime is current on the thread that + builds it and `async-io` with an I/O thread of its own otherwise. ashpd keeps + one session connection for the whole process (a static in its `proxy.rs`), so + the first ashpd caller decides the executor of every later portal call: when + a tokio caller builds it first, a later `Proxy::new` from a thread with no + runtime (a GPUI foreground task) panics with "there is no reactor running" + (`get_property("version")` sets up zbus's property cache, which calls + `tokio::task::spawn`). The move of e325b65 left both compiled in + (voelin-platform's notify-rust asked zbus for `tokio`, `z-with-tokio`), and + the merged binary was safe only because GPUI's colour-scheme call builds the + connection before the engine's portal calls do. *Fixed* (the Opus gate's + option a): notify-rust asks for `z` (`async-io`), so zbus is compiled with + `async-io` alone in every binary, the Slint app's included, and every + connection has an I/O thread of its own whoever builds it first + (`cargo tree -e features -i zbus` names `tokio` nowhere; voelin-platform's + own test dependency on zbus is `async-io` as well, since a test binary with + both features shows the panic again). Two tests in voelin-platform + (`tests/proxy.rs`, each on a private D-Bus bus) call ashpd from a thread + with no tokio runtime and get the answer, the second after a tokio caller + built the connection; that one fails with the panic above on the tree + before the change. - **Size and compile time.** In the root workspace, which already carries Slint's and the codecs' dependencies, gpui-kit adds 279 entries to `Cargo.lock` (208 new names; about 195 packages on Linux), and cargo deny