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

[stable-3.5] Ensure the dispatch source only gets deallocated after the dispatch_source_cancel is done, avoiding crashing of the Finder Sync Extension on macOS #4696

Merged
merged 1 commit into from
Jul 1, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,24 @@ - (void)closeConnection
NSLog(@"Closing connection.");

if(self.readSource) {
// Since dispatch_source_cancel works asynchronously, if we deallocate the dispatch source here then we can
// cause a crash. So instead we strongly hold a reference to the read source and deallocate it asynchronously
// with the handler.
__block dispatch_source_t previousReadSource = self.readSource;
dispatch_source_set_cancel_handler(self.readSource, ^{
previousReadSource = nil;
});
dispatch_source_cancel(self.readSource);
// The readSource is still alive due to the other reference and will be deallocated by the cancel handler
self.readSource = nil;
}

if(self.writeSource) {
// Same deal with the write source
__block dispatch_source_t previousWriteSource = self.writeSource;
dispatch_source_set_cancel_handler(self.writeSource, ^{
previousWriteSource = nil;
});
dispatch_source_cancel(self.writeSource);
self.writeSource = nil;
}
Expand Down