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

Conversation

complexspaces
Copy link
Contributor

This PR implements a solution for #1282 by adding an Objective-C autorelease pool around the CoreFoundation FFI calls sysinfo uses to obtain disk information. This was chosen over a tempoary thread because spawning a thread is a much heavier operation (shared memory, stack allocations, etc). It also seems better then a worker thread because longer-lived apps that don't call this operation much might not want an extra thread living forever. In addition to all these approaches, if people are using sysinfo code in a traditional cocoa app then this thread would be extra pointless as the built-in runloops would be clearing autorelease'd values out already.

In the future this pool could be moved to a more reusable context to support multiple refreshes before draining, but this is a performance issue and not a correctness one. Therefore, it is left as future work.

Closes #1282

@GuillaumeGomez
Copy link
Owner

Just one change but otherwise looks good to me. 👍

/// 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 {
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.

@GuillaumeGomez
Copy link
Owner

Thanks a lot! I'll make a release shortly.

@GuillaumeGomez GuillaumeGomez merged commit 97267b1 into GuillaumeGomez:master Aug 28, 2024
67 checks passed
@complexspaces complexspaces deleted the pool-apple-disk-refreshing branch September 3, 2024 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possible memory leak in Disks::refresh_list
2 participants