Description
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.
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
- ID: 572ef6c6-5066-4a14-76a0-c3fb4b62e604
- Version Independent ID: 9c754da1-c234-fb12-580b-0fcf9ffe2c20
- Platform ID: de77d8c7-6af8-0b64-9357-30e46ab49cbd
- Content: Implement a Dispose method - .NET
- Content Source: docs/standard/garbage-collection/implementing-dispose.md
- Service: dotnet-fundamentals
- GitHub Login: @gewarren
- Microsoft Alias: gewarren