From 6b55a6f0e1dec0c0c781d78ce2dc5497ece3f826 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Tue, 30 Apr 2024 23:31:59 -0600 Subject: [PATCH] Retain run loop (#11241) Contributes: #11168 https://developer.apple.com/documentation/corefoundation/1542428-cfrunloopgetcurrent implies that we should be `CFRetain`ing the run loop. Lets do that, and see if it reduces the number of crashes we see. Release Notes: - (maybe) Fix a rare crash in watching settings files. --- Cargo.lock | 1 + crates/fsevent/Cargo.toml | 1 + crates/fsevent/src/fsevent.rs | 4 +++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 3620ea1aac53f5407f3411951327f3a9774734f8..a28fea414f06de5f2eb163011f583e13ec3e4f28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4105,6 +4105,7 @@ name = "fsevent" version = "0.1.0" dependencies = [ "bitflags 2.4.2", + "core-foundation", "fsevent-sys 3.1.0", "parking_lot", "tempfile", diff --git a/crates/fsevent/Cargo.toml b/crates/fsevent/Cargo.toml index 6a5a01843d14129a4c13499f2245801e3c143906..957917bf92ea2e0683ad4387a843710cc3063a79 100644 --- a/crates/fsevent/Cargo.toml +++ b/crates/fsevent/Cargo.toml @@ -17,6 +17,7 @@ bitflags.workspace = true parking_lot.workspace = true [target.'cfg(target_os = "macos")'.dependencies] +core-foundation.workspace = true fsevent-sys = "3.0.2" [dev-dependencies] diff --git a/crates/fsevent/src/fsevent.rs b/crates/fsevent/src/fsevent.rs index ef113b6de2a2cb5918120350f3ad65e58c623da5..533bb9097984f0a59007de03616cec5890a7bd27 100644 --- a/crates/fsevent/src/fsevent.rs +++ b/crates/fsevent/src/fsevent.rs @@ -120,7 +120,8 @@ impl EventStream { { self.state.callback = Some(Box::new(f)); unsafe { - let run_loop = cf::CFRunLoopGetCurrent(); + let run_loop = + core_foundation::base::CFRetain(cf::CFRunLoopGetCurrent()) as *mut c_void; { let mut state = self.lifecycle.lock(); match *state { @@ -248,6 +249,7 @@ impl Drop for Handle { if let Lifecycle::Running(run_loop) = *state { unsafe { cf::CFRunLoopStop(run_loop); + cf::CFRelease(run_loop) } } *state = Lifecycle::Stopped;