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

The dispose pattern docs should not use an IntPtr.Zero handle #44201

Open
antonfirsov opened this issue Jan 9, 2025 · 0 comments
Open

The dispose pattern docs should not use an IntPtr.Zero handle #44201

antonfirsov opened this issue Jan 9, 2025 · 0 comments
Labels
⌚ Not Triaged Not triaged

Comments

@antonfirsov
Copy link
Member

antonfirsov commented Jan 9, 2025

Describe the issue or suggestion

The following doc was likely created in .NET Framework times, where the BaseClassWithSafeHandle code example creates and closes an IntPTr.Zero handle, which is NOP on Windows: https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose#implement-the-dispose-pattern

The problem is that on Unix FD 0 corresponds to stdnin which means that disposing BaseClassWithSafeHandle will close the standard input. What's even worse, that it will free up FD 0 for usage in other places, eg. Socket meaning that creating and disposing BaseClassWithSafeHandle several times may close the handle under an existing socket leading to unwanted side effects.

There were cases where users left the dummy code from the sample in their codebase, eventually leading to hard-to-diagnose production errors, see dotnet/runtime#56750, dotnet/runtime#64305, fabian-blum/AspNetCore.Identity.LiteDB#14.

We should change this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⌚ Not Triaged Not triaged
Projects
None yet
Development

No branches or pull requests

1 participant