Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(desktop): accidental locking of the broker (#4544)

Elijah Potter8057d2328bef
diff --git a/harper-desktop/src-tauri/src/commands.rs b/harper-desktop/src-tauri/src/commands.rs
index 54738c397..264c76661 100644
--- a/harper-desktop/src-tauri/src/commands.rs
+++ b/harper-desktop/src-tauri/src/commands.rs
@@ -12,7 +12,7 @@ use harper_core::{
linting::FlatConfig,
spell::{Dictionary, MutableDictionary},
};
-use std::sync::{Arc, Mutex as StdMutex};
+use std::sync::{Arc, RwLock};
use tauri::ipc::Invoke;
use tauri::{Manager, Runtime, State};
use tokio::sync::Mutex;
@@ -256,11 +256,11 @@ async fn add_to_dictionary(
#[tauri::command]
async fn get_integrations(
config: State<'_, Arc<Mutex<Config>>>,
- broker: State<'_, StdMutex<PlatformBroker>>,
+ broker: State<'_, RwLock<PlatformBroker>>,
) -> Result<Vec<IntegrationView>, String> {
let integrations = config.lock().await.integrations.clone();
let broker = broker
- .lock()
+ .read()
.map_err(|error| format!("Failed to read platform broker: {error}"))?;
Ok(integrations
@@ -345,9 +345,9 @@ async fn get_application_icon_data_url<R: Runtime>(
app_handle: tauri::AppHandle<R>,
) -> Result<String, String> {
tauri::async_runtime::spawn_blocking(move || {
- let broker = app_handle.state::<StdMutex<PlatformBroker>>();
+ let broker = app_handle.state::<RwLock<PlatformBroker>>();
let icon_png = broker
- .lock()
+ .read()
.map_err(|error| format!("Failed to read platform broker: {error}"))?
.application_icon_png(&bundle_id)?;
let encoded = general_purpose::STANDARD.encode(icon_png);
@@ -360,9 +360,9 @@ async fn get_application_icon_data_url<R: Runtime>(
#[tauri::command]
fn get_accessibility_permission_status(
- broker: State<'_, StdMutex<PlatformBroker>>,
+ broker: State<'_, RwLock<PlatformBroker>>,
) -> AccessibilityPermissionStatus {
- match broker.lock() {
+ match broker.read() {
Ok(broker) => broker.accessibility_permission_status(),
Err(error) => {
eprintln!("Failed to read platform broker: {error}");
@@ -373,9 +373,9 @@ fn get_accessibility_permission_status(
#[tauri::command]
fn request_accessibility_permission(
- broker: State<'_, StdMutex<PlatformBroker>>,
+ broker: State<'_, RwLock<PlatformBroker>>,
) -> AccessibilityPermissionStatus {
- match broker.lock() {
+ match broker.read() {
Ok(broker) => broker.request_accessibility_permission(),
Err(error) => {
eprintln!("Failed to read platform broker: {error}");
@@ -423,12 +423,9 @@ pub(crate) async fn stop_highlighter_service(
}
#[tauri::command]
-fn launch_app(
- bundle_id: String,
- broker: State<'_, StdMutex<PlatformBroker>>,
-) -> Result<(), String> {
+fn launch_app(bundle_id: String, broker: State<'_, RwLock<PlatformBroker>>) -> Result<(), String> {
broker
- .lock()
+ .read()
.map_err(|error| format!("Failed to read platform broker: {error}"))?
.launch_app_bundle(&bundle_id)
}
@@ -436,10 +433,10 @@ fn launch_app(
#[tauri::command]
fn search_apps(
query: String,
- broker: State<'_, StdMutex<PlatformBroker>>,
+ broker: State<'_, RwLock<PlatformBroker>>,
) -> Result<Vec<AppSearchResult>, String> {
broker
- .lock()
+ .read()
.map_err(|error| format!("Failed to read platform broker: {error}"))?
.search_apps(&query)
}
diff --git a/harper-desktop/src-tauri/src/lib.rs b/harper-desktop/src-tauri/src/lib.rs
index 60b6a0ee5..a44afcd0c 100644
--- a/harper-desktop/src-tauri/src/lib.rs
+++ b/harper-desktop/src-tauri/src/lib.rs
@@ -18,7 +18,7 @@ use std::io::stderr;
use std::{
cell::RefCell,
rc::Rc,
- sync::{Arc, Mutex as StdMutex},
+ sync::{Arc, Mutex as StdMutex, RwLock},
};
use tauri::Manager as _;
use tracing::{Level, error};
@@ -84,7 +84,7 @@ pub(crate) type PlatformBroker = os_broker::NoopBroker;
/// The Tauri process stores its single broker as managed state, while the highlighter subprocess
/// creates its own broker because it is a separate process.
fn platform_broker(
- is_integration_enabled: impl FnMut(&str) -> bool + Send + 'static,
+ is_integration_enabled: impl FnMut(&str) -> bool + Send + Sync + 'static,
) -> PlatformBroker {
#[cfg(any(target_os = "macos", target_os = "windows"))]
{
@@ -100,9 +100,9 @@ fn platform_broker(
fn warm_app_search_cache(app: tauri::AppHandle) {
tauri::async_runtime::spawn_blocking(move || {
- let broker = app.state::<StdMutex<PlatformBroker>>();
+ let broker = app.state::<RwLock<PlatformBroker>>();
let result = broker
- .lock()
+ .read()
.map_err(|error| format!("failed to read platform broker: {error}"))
.and_then(|broker| broker.search_apps("").map(|_| ()));
@@ -182,7 +182,7 @@ pub fn run_tauri() {
.manage(config)
.manage(desktop_updater::DesktopUpdater::default())
.manage(highlighter_service)
- .manage(StdMutex::new(broker))
+ .manage(RwLock::new(broker))
.manage(async_runtime)
.plugin(tauri_plugin_autostart::init(
tauri_plugin_autostart::MacosLauncher::LaunchAgent,
@@ -456,8 +456,8 @@ struct IntegrationState {
/// Standalone callers can approve registration locally without IPC.
fn integration_callback(
state: Arc<StdMutex<IntegrationState>>,
- mut resolve: impl FnMut(&str) -> bool + Send + 'static,
-) -> impl FnMut(&str) -> bool + Send + 'static {
+ mut resolve: impl FnMut(&str) -> bool + Send + Sync + 'static,
+) -> impl FnMut(&str) -> bool + Send + Sync + 'static {
move |bundle_id| {
let bundle_id = bundle_id.trim();
if bundle_id.is_empty() {
diff --git a/harper-desktop/src-tauri/src/mac_broker/mod.rs b/harper-desktop/src-tauri/src/mac_broker/mod.rs
index ba287e013..ca0726d1b 100644
--- a/harper-desktop/src-tauri/src/mac_broker/mod.rs
+++ b/harper-desktop/src-tauri/src/mac_broker/mod.rs
@@ -63,7 +63,7 @@ pub struct MacBroker {
/// Tauri requires a Send broker; retained AX handles must stay on their capturing thread.
/// The wrapper enforces that restriction for access and drop. Only the highlighter populates it.
last_focused: Option<SendWrapper<FocusedTarget>>,
- is_integration_enabled: Box<dyn FnMut(&str) -> bool + Send>,
+ is_integration_enabled: Box<dyn FnMut(&str) -> bool + Send + Sync>,
application_icon_cache: Mutex<HashMap<String, Vec<u8>>>,
window_movement: Option<WindowMovementState>,
accessibility_activation: Option<AccessibilityActivationState>,
@@ -72,7 +72,7 @@ pub struct MacBroker {
impl MacBroker {
/// Creates a broker with an app policy that may register newly encountered bundle IDs.
/// The policy is called before reading the app's text and may change as settings are refreshed.
- pub fn new(is_integration_enabled: impl FnMut(&str) -> bool + Send + 'static) -> Self {
+ pub fn new(is_integration_enabled: impl FnMut(&str) -> bool + Send + Sync + 'static) -> Self {
Self {
last_focused: None,
is_integration_enabled: Box::new(is_integration_enabled),
diff --git a/harper-desktop/src-tauri/src/windows_broker/mod.rs b/harper-desktop/src-tauri/src/windows_broker/mod.rs
index 52cbbc399..2840de065 100644
--- a/harper-desktop/src-tauri/src/windows_broker/mod.rs
+++ b/harper-desktop/src-tauri/src/windows_broker/mod.rs
@@ -31,13 +31,13 @@ mod automation_service;
pub struct WindowsBroker {
service: Arc<Mutex<AutomationService>>,
- is_integration_enabled: Box<dyn FnMut(&str) -> bool + Send>,
+ is_integration_enabled: Box<dyn FnMut(&str) -> bool + Send + Sync>,
}
impl WindowsBroker {
/// Creates a broker with an app policy that may register newly encountered executable paths.
/// The policy is called before reading the app's text and may change as settings are refreshed.
- pub fn new(is_integration_enabled: impl FnMut(&str) -> bool + Send + 'static) -> Self {
+ pub fn new(is_integration_enabled: impl FnMut(&str) -> bool + Send + Sync + 'static) -> Self {
Self {
service: Arc::new(Mutex::new(AutomationService::create_and_start())),
is_integration_enabled: Box::new(is_integration_enabled),