Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(focus-tracker): drive list_monitors() via captured tokio Handle on Windows

Louis Beaumontde42857842a9
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
index 6ccfe44ff..7dc91d7de 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.lock
@@ -9726,7 +9726,7 @@ dependencies = [
[[package]]
name = "screenpipe-app"
-version = "2.4.19"
+version = "2.4.20"
dependencies = [
"anyhow",
"arboard",
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
index ec657f264..453f65b20 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "screenpipe-app"
-version = "2.4.19"
+version = "2.4.20"
description = "24/7 memory for your desktop. 100% local."
authors = ["you"]
license = ""
diff --git a/crates/screenpipe-engine/src/focus_tracker/windows.rs b/crates/screenpipe-engine/src/focus_tracker/windows.rs
index 60c7c4601..418e59859 100644
--- a/crates/screenpipe-engine/src/focus_tracker/windows.rs
+++ b/crates/screenpipe-engine/src/focus_tracker/windows.rs
@@ -129,6 +129,11 @@ struct Inner {
current: AtomicU32,
stop_flag: AtomicBool,
unknown_emitted: Mutex<bool>,
+ // Handle to the tokio runtime captured at start(). The WinEvent callback
+ // runs on the Win32 message-pump thread (no tokio context), so we drive
+ // async calls via this handle instead of `futures::executor::block_on`,
+ // which would panic on `tokio::task::spawn_blocking` inside list_monitors.
+ runtime: tokio::runtime::Handle,
}
impl Inner {
@@ -211,17 +216,18 @@ pub struct WindowsFocusTracker {
impl WindowsFocusTracker {
#[cfg(target_os = "windows")]
pub fn start() -> Result<Self> {
+ let handle = tokio::runtime::Handle::try_current()
+ .map_err(|e| anyhow::anyhow!("no tokio runtime for focus tracker: {e}"))?;
+
let (tx, _) = broadcast::channel::<FocusEvent>(16);
let inner = Arc::new(Inner {
tx,
current: AtomicU32::new(0),
stop_flag: AtomicBool::new(false),
unknown_emitted: Mutex::new(false),
+ runtime: handle.clone(),
});
- let handle = tokio::runtime::Handle::try_current()
- .map_err(|e| anyhow::anyhow!("no tokio runtime for focus tracker: {e}"))?;
-
win_event_shared::set(&inner);
// Spawn the WinEvent message-loop thread.
@@ -293,12 +299,15 @@ fn run_win_event_observer() {
if inner.stop_flag.load(Ordering::Relaxed) {
return;
}
- // Resolve the monitor list on-the-fly. `list_monitors()` is async —
- // use a short-lived tokio runtime via `futures::executor::block_on`.
- // Accepts some overhead for the rare case of very fast focus changes;
- // the hook is not called at high frequency in practice (a few per
- // second at most during window switching).
- let monitors = futures::executor::block_on(screenpipe_screen::monitor::list_monitors());
+ // `list_monitors()` is async and internally uses `tokio::task::spawn_blocking`,
+ // so it MUST run on the tokio runtime captured at start(). This callback fires
+ // on the Win32 message-pump thread, which has no tokio context — driving the
+ // future via the captured `Handle` is the only safe option here. Using
+ // `futures::executor::block_on` would panic ("no reactor running") inside
+ // `spawn_blocking`.
+ let monitors = inner
+ .runtime
+ .block_on(screenpipe_screen::monitor::list_monitors());
inner.resolve_and_emit(&monitors);
}