Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Wrap Apple disk refreshing in autorelease pool to avoid system caching #1344

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
38 changes: 37 additions & 1 deletion src/unix/apple/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ impl crate::DisksInner {

pub(crate) fn refresh_list(&mut self) {
unsafe {
get_list(&mut self.disks);
// SAFETY: We don't keep any Objective-C objects around because we
// don't make any direct Objective-C calls in this code.
with_autorelease(|| {
get_list(&mut self.disks);
})
}
}

Expand Down Expand Up @@ -431,3 +435,35 @@ unsafe fn new_disk(
},
})
}


/// Calls the provided closure in the context of a new autorelease pool that is drained
/// before returning.
///
/// ## SAFETY:
/// You must not return an Objective-C object that is autoreleased from this function since it
/// will be freed before usable.
unsafe fn with_autorelease<T, F: FnOnce() -> T>(call: F) -> T {
// NB: This struct exists to help prevent memory leaking if `call` were to panic.
// Otherwise, the call to `objc_autoreleasePoolPop` would never be made as the stack unwinds.
// `Drop` destructors for existing types on the stack are run during unwinding, so we can
// ensure the autorelease pool is drained by using a RAII pattern here.
struct DrainPool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of having this struct? Couldn't both ffi functions be called before and after call()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This to to avoid any unnecessary leaking if get_list (or any other function in the future) panics and unwinds the stack. Without it the pop function wouldn't be called in these cases, leading to leaks. With this, the destructor will be called in the process. A lot of programs/libraries wrap calls in std::panic::catch_unwind so a panic here is not guaranteed to terminate the thread (and therefore free its memory) without something like this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum... I wonder here if it isn't too much considering this is sysinfo code only... Well, I'll let it up to you. If you want to stick with this version, please add a comment on the struct explaining why we do it this way please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the comment to provide context to future readers of this code.

ctx: *mut c_void,
}

impl Drop for DrainPool {
fn drop(&mut self) {
// SAFETY: We have not manipulated `pool_ctx` since it was received from a corresponding
// pool push call.
unsafe { ffi::objc_autoreleasePoolPop(self.ctx) }
}
}

// SAFETY: Creating a new pool is safe in any context. They can be arbitrarily nested
// as long as pool objects are not used in deeper layers, but we only have one and don't
// allow it to leave this scope.
let _pool_ctx = DrainPool { ctx: unsafe { ffi::objc_autoreleasePoolPush() } };
call()
// Pool is drained here before returning
}
7 changes: 7 additions & 0 deletions src/unix/apple/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cfg_if! {
array::CFArrayRef, dictionary::CFDictionaryRef, error::CFErrorRef, string::CFStringRef,
url::CFURLRef,
};
use std::ffi::c_void;

#[link(name = "CoreFoundation", kind = "framework")]
extern "C" {
Expand All @@ -32,6 +33,12 @@ cfg_if! {
pub static kCFURLVolumeIsInternalKey: CFStringRef;
pub static kCFURLVolumeIsBrowsableKey: CFStringRef;
}

#[link(name = "objc", kind = "dylib")]
extern "C" {
pub fn objc_autoreleasePoolPop(pool: *mut c_void);
pub fn objc_autoreleasePoolPush() -> *mut c_void;
}
}
}

Expand Down