-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
More reliably clean up more SafeHandle instances #71991
More reliably clean up more SafeHandle instances #71991
Conversation
Tagging subscribers to this area: @dotnet/area-system-security, @vcsjones Issue DetailsThis calls Dispose on SafeHandles (or things wrapping SafeHandles) that were otherwise being left for finalization:
These were found primarily via two means:
There's a lot more that can be cleaned up using the SafeHandle instrumentation, but I'm pausing here for now. The System.IO.Pipes, System.IO.FileSystem, and System.Security.Cryptography tests are clean on Windows (with the exception of a few tests that are purposefully leaving objects for finalization to test finalization code paths).
|
0f67355
to
b1cfee5
Compare
...es/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcDsa.ImportExport.cs
Show resolved
Hide resolved
src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs
Outdated
Show resolved
Hide resolved
...es/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.cs
Outdated
Show resolved
Hide resolved
src/libraries/Common/tests/System/IO/Compression/CompressionStreamUnitTestBase.cs
Show resolved
Hide resolved
4e05508
to
fd8d46d
Compare
The runtime and SafeHandle.cs changes LGTM. I have done cursory review of the rest. |
src/libraries/Common/tests/System/IO/Compression/CompressionStreamUnitTestBase.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, other than braceless usings in product code (which I believed we had banned in style outside of tests, but I guess not?)
I don't know why we'd ban them. |
Yay, glad you added this switch. Do you plan to make an "up for grabs" issue to continue, in case folks are interested? eg., with a checkbox for each library that has hits (if you have that data gathered)? Can you imagine us getting clean - I'm guessing there are test cases for finalizers, though. How much slower is it to run tests when the switch is flipped? I assume it's expensive to gather stacks. |
A lot... starting with the fact that it's only built into a debug/checked runtime, and then layering on a stack walk for every safe handle created.
Without workarounds that'd be challenging, e.g. because of tests for finalizers as you say, tests that purposefully allow resources to not be disposed to avoid race conditions, etc.
I don't. |
This calls Dispose on SafeHandles (or things wrapping SafeHandles) that were otherwise being left for finalization: 1. Some of these are fixing finalization happening even on success paths (typically where the implementation isn't disposing of something that directly or indirectly wraps a SafeHandle). 2. Some of these are fixing finalization happening for invalid SafeHandles. 3. Some of these are fixing tests to finalize less. My goal with fixing the tests was to eliminate the noise in order to find instances of the other two cases. These were found primarily via two means: - Debug-instrumentation in SafeHandle to log when one is finalized. This instrumentation is built into debug/checked builds of SafeHandle and requires setting the DEBUG_SAFEHANDLE_FINALIZATION environment variable to "1". - Auditing use of SafeHandle.IsInvalid There's a lot more that can be cleaned up using the SafeHandle instrumentation, but I'm pausing here for now. The System.IO.Pipes, System.IO.FileSystem, and System.Security.Cryptography tests are clean on Windows.
fd8d46d
to
767ee06
Compare
Makes sense -- probably we can use this flag on rare occasions, eg., having written a new feature with non trivial use of safehandles. |
If you have to spin again, Steve, I'm indifferent about keeping or removing the version in runtime/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeX509Handles.Unix.cs Lines 12 to 26 in 388dd6c
|
Me, too. I will need to resolve a conflict, so I can remove it if you like. |
Eh, if you're indifferent and I'm indifferent, let's leave it, so I can have a "smaller than the whole world" experience :) |
Possible regression: dotnet/perf-autofiling-issues#6973 |
Since CryptoConfig.CreateFromName is just a lookup in ConcurrentDictionary, reflection to find a suitable ctor, and then reflection invocation of said ctor, I don't think this change was involved. |
@bartonjs, I did touch this: That said, I'm not sure how much a few hundred nanoseconds on this particular method matters. We don't want folks using this method anyway, right? |
Yeah, I had that as a suspect, but then looked at the perf test: It goes straight to https://source.dot.net/#System.Security.Cryptography/System/Security/Cryptography/CryptoConfig.cs,2ea13b091ee4b642, not through the generic wrapper. |
In that case, yeah, I'm not on the hook :) |
This calls Dispose on SafeHandles (or things wrapping SafeHandles) that were otherwise being left for finalization:
These were found primarily via two means:
There's a lot more that can be cleaned up using the SafeHandle instrumentation, but I'm pausing here for now. The System.IO.Pipes, System.IO.FileSystem, and System.Security.Cryptography tests are clean on Windows (with the exception of a few tests that are purposefully leaving objects for finalization to test finalization code paths).