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

Release GCHandle in FSEventStreamContext release callback #52275

Merged
merged 1 commit into from
May 5, 2021

Conversation

jkotas
Copy link
Member

@jkotas jkotas commented May 4, 2021

The callbacks are sometimes delivered even after the FSEventStream is disposed

Fixes #30056

@ghost
Copy link

ghost commented May 4, 2021

Tagging subscribers to this area: @carlossanlop
See info in area-owners.md if you want to be subscribed.

Issue Details

The callbacks are sometimes delivered even after the FSEventStream is disposed

Fixes #30056

Author: jkotas
Assignees: -
Labels:

area-System.IO

Milestone: -

The callbacks are sometimes delivered even after the FSEventStream is disposed
@@ -78,8 +78,8 @@ internal struct FSEventStreamContext
{
public CFIndex version;
public IntPtr info;
public IntPtr retainFunc;
public IntPtr releaseFunc;
public IntPtr retain;
Copy link
Member Author

Choose a reason for hiding this comment

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

Match the names in Apple SDK

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

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

LGTM, thank you @jkotas

Are there any other places where we are passing pointers to functions to native methods that we should revisit and ensure we do it always right as you just did here?

Not related to this particular PR, but do we have any guidance on when we should be trying to implement new features that call native methods using 100% managed code (and function pointers) vs extending the native PAL layer? An example: we will be adding symbolic links support very soon. Let's say there is an abc method that works on every Unix system (no need for #if defines). Should we use function pointers? Or add another method to pal_io.c?

@jkotas
Copy link
Member Author

jkotas commented May 5, 2021

Are there any other places where we are passing pointers to functions to native methods that we should revisit and ensure we do it always right as you just did here?

The OS API in this case has a complex lifetime semantics that are not very well documented. We do not call Apple API like this anywhere else.

Also, I am not 100% sure whether this will actually fix the reliability issues that we facing with this component. We will only know after it runs for some time.

do we have any guidance on when we should be trying to implement new features that call native methods using 100% managed code (and function pointers) vs extending the native PAL layer?

The guidance is written down here: https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/interop-guidelines.md#unix-shims

Or add another method to pal_io.c?

The symlinks support should add more methods to pal_io.c per the above guidelines.

Should we use function pointers?

For simple calls into unmanaged code, we should be using a simple DllImport. No function pointers needed.

Otherwise, the function pointers should be prefered over marshaled delegates where possible. Using function pointers for interop is more performant, more AOT friendly, and it avoids the "callback on collected delegate" trap that one often falls into with marshaled delegates.

I have been converting marshaled delegates to function pointers throughout the repo. See #52192 that is still waiting for review - would you like to volunteer?

@jkotas jkotas merged commit 1e99834 into dotnet:main May 5, 2021
@jkotas jkotas deleted the watcher-fix branch May 5, 2021 13:29
@runfoapp runfoapp bot mentioned this pull request May 5, 2021
@karelz karelz added this to the 6.0.0 milestone May 20, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jun 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SafeHandle use-after-dispose in FileSystemWatcher on OSX
3 participants