Skip to content

Commit 0935472

Browse files
Michael Fethclaude
andcommitted
fix(desktop): hold the sleep block through a safe wrapper, not local FFI
Review follow-up on PR #6036. AGENTS.md states "No `unsafe` code" without qualification, and the previous revision of this branch added Win32 FFI (PowerCreateRequest / PowerSetRequest / PowerClearRequest plus integer round-tripping of the raw HANDLE). That is the policy as written, so the local FFI goes. prevent_sleep.rs now reaches the platform mechanism through keepawake, which also retires the IOKit `extern "C"` block and the CoreFoundation string plumbing this module already carried on main. The file goes from 8 unsafe blocks to zero. The thread-lifetime constraint that ruled out SetThreadExecutionState still holds, so it is solved structurally rather than by choice of API: a dedicated thread creates the guard, parks on a channel, and drops the guard when the channel disconnects. SleepBlock::drop drops the sender and then JOINS that thread, so release is synchronous -- when drop returns, the OS request is provably gone, released on the same thread that took it. acquire and release can keep arriving on three different Tauri worker threads without the request evaporating or leaking. Dependency cost, measured rather than assumed: keepawake adds exactly ONE package to Cargo.lock. Every transitive dependency it names (windows, zbus, objc2-core-foundation, objc2-io-kit, derive_builder, thiserror, cfg-if) was already in the tree, and the lock diff contains no version changes. The Win32_System_Power feature added to windows-sys by the previous revision is removed, returning that line to its state on main. keepawake is target-gated to macOS and Windows. Its Linux path needs a live D-Bus session, and Linux here is a deliberate no-op, so gating keeps a Linux build free of a new runtime requirement. Verified: cargo check clean; cargo fmt clean; prevent_sleep tests 4/4 including ensure_block_holds_a_real_os_block_and_is_idempotent, which takes a real OS block through keepawake and asserts a second acquire reuses the same worker thread rather than stranding one; full desktop-tauri suite 2319 passed / 1 failed, that one being managed_agents::runtime::tests:: claude_spawn_uses_the_probed_cli_executable, which mutates process-global PATH and races other tests in the same process -- it passes in isolation and this branch does not touch that module. Not clean, and pre-existing: clippy -D warnings fails on Windows in buzz-terminal (unused std::io / Instant / portable_pty::Child imports that are only used behind #[cfg(unix)]). That crate is not in this diff. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Signed-off-by: Michael Feth <[email protected]>
1 parent a99170b commit 0935472

3 files changed

Lines changed: 154 additions & 210 deletions

File tree

‎desktop/src-tauri/Cargo.lock‎

Lines changed: 42 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎desktop/src-tauri/Cargo.toml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ notify-rust = "4"
5151
webkit2gtk = { version = "=2.0.2", features = ["v2_22"] }
5252

5353
[target.'cfg(target_os = "macos")'.dependencies]
54+
# Safe wrapper over the platform idle-sleep APIs (IOKit here, Win32 power
55+
# requests on Windows). Target-gated to the two platforms that have a
56+
# mechanism: Linux stays a documented no-op, so pulling keepawake's zbus path
57+
# in there would add a runtime D-Bus requirement for behaviour we do not want.
58+
keepawake = "0.6"
5459
block2 = { version = "0.6", default-features = false, features = ["std"] }
5560
objc2 = { version = "0.6.4", default-features = false }
5661
objc2-app-kit = { version = "0.3.2", default-features = false, features = ["NSEvent", "NSHapticFeedback", "NSMenu", "NSMenuItem", "NSStatusItem", "block2"] }
@@ -63,7 +68,8 @@ user-idle = { version = "0.6", default-features = false }
6368
plist = "1"
6469

6570
[target.'cfg(windows)'.dependencies]
66-
windows-sys = { version = "0.61", features = ["Win32_Security", "Win32_Storage_FileSystem", "Win32_System_JobObjects", "Win32_System_Power", "Win32_System_Registry", "Win32_System_Threading", "Win32_Foundation"] }
71+
keepawake = "0.6"
72+
windows-sys = { version = "0.61", features = ["Win32_Security", "Win32_Storage_FileSystem", "Win32_System_JobObjects", "Win32_System_Registry", "Win32_System_Threading", "Win32_Foundation"] }
6773
keyring = { version = "3.6.3", default-features = false, features = ["windows-native", "vendored"], optional = true }
6874
user-idle = { version = "0.6", default-features = false }
6975

0 commit comments

Comments
 (0)