Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(macos): fix race condition in wkwebview implementation of cookie fetching #1486

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/fix-cookies-deadlock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Fix `Webview::cookies` and `Webview::cookies_for_url` deadlock on macOS.
20 changes: 14 additions & 6 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ use std::{
ptr::{null_mut, NonNull},
str::{self, FromStr},
sync::{Arc, Mutex},
time::Duration,
};

#[cfg(feature = "mac-proxy")]
Expand Down Expand Up @@ -1054,24 +1055,31 @@ unsafe fn window_position(view: &NSView, x: i32, y: i32, height: f64) -> CGPoint
CGPoint::new(x as f64, frame.size.height - y as f64 - height)
}

/// Wait synchronously for the NSRunLoop to run until a receiver has a message.
unsafe fn wait_for_blocking_operation<T>(rx: std::sync::mpsc::Receiver<T>) -> Result<T> {
let interval = 0.0002;
let interval = Duration::from_millis(2);
let interval_as_secs = interval.as_secs_f64();
let limit = 1.;
let mut elapsed = 0.;
// run event loop until we get the response back, blocking for at most 3 seconds
loop {
let rl = objc2_foundation::NSRunLoop::mainRunLoop();
let d = NSDate::dateWithTimeIntervalSinceNow(interval);
rl.runUntilDate(&d);
if let Ok(response) = rx.try_recv() {
if let Ok(response) = rx.recv_timeout(interval) {
return Ok(response);
}
elapsed += interval;
elapsed += interval_as_secs;
if elapsed >= limit {
return Err(Error::Io(std::io::Error::new(
std::io::ErrorKind::TimedOut,
"timed out waiting for cookies response",
)));
}

// Go progress the event loop if we didn't get the result
let rl = objc2_foundation::NSRunLoop::mainRunLoop();
let limit_date = NSDate::dateWithTimeIntervalSinceNow(interval_as_secs);

let mode = NSString::from_str("NSDefaultRunLoopMode");

rl.acceptInputForMode_beforeDate(&mode, &limit_date);
}
}
Loading