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

More reliably clean up more SafeHandle instances #71991

Merged
merged 5 commits into from
Jul 13, 2022

Conversation

stephentoub
Copy link
Member

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. Their IsInvalid is true, but they still get finalized.
  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 (with the exception of a few tests that are purposefully leaving objects for finalization to test finalization code paths).

@ghost
Copy link

ghost commented Jul 12, 2022

Tagging subscribers to this area: @dotnet/area-system-security, @vcsjones
See info in area-owners.md if you want to be subscribed.

Issue Details

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. Their IsInvalid is true, but they still get finalized.
  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 (with the exception of a few tests that are purposefully leaving objects for finalization to test finalization code paths).

Author: stephentoub
Assignees: -
Labels:

area-System.Security

Milestone: -

@stephentoub stephentoub force-pushed the moresafehandlefinalization branch from 0f67355 to b1cfee5 Compare July 12, 2022 05:03
@stephentoub stephentoub force-pushed the moresafehandlefinalization branch from 4e05508 to fd8d46d Compare July 12, 2022 13:38
@jkotas
Copy link
Member

jkotas commented Jul 12, 2022

The runtime and SafeHandle.cs changes LGTM. I have done cursory review of the rest.

Copy link
Member

@bartonjs bartonjs left a 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?)

@stephentoub
Copy link
Member Author

other than braceless usings in product code (which I believed we had banned in style outside of tests

I don't know why we'd ban them.

@danmoseley
Copy link
Member

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.

@stephentoub
Copy link
Member Author

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.

Can you imagine us getting clean - I'm guessing there are test cases for finalizers, though.

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.

if you have that data gathered

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.
@stephentoub stephentoub force-pushed the moresafehandlefinalization branch from fd8d46d to 767ee06 Compare July 12, 2022 20:55
@danmoseley
Copy link
Member

Makes sense -- probably we can use this flag on rare occasions, eg., having written a new feature with non trivial use of safehandles.

@bartonjs
Copy link
Member

If you have to spin again, Steve, I'm indifferent about keeping or removing the version in

#if DEBUG
private static readonly bool s_captureTrace =
Environment.GetEnvironmentVariable("DEBUG_SAFEX509HANDLE_FINALIZATION") != null;
private readonly StackTrace? _stacktrace =
s_captureTrace ? new StackTrace(fNeedFileInfo: true) : null;
~SafeX509Handle()
{
if (s_captureTrace)
{
Console.WriteLine($"0x{handle.ToInt64():x} {_stacktrace?.ToString() ?? "no stacktrace..."}");
}
}
#endif
that I left in from getting Linux PKCS7 tidy.

@stephentoub
Copy link
Member Author

I'm indifferent about keeping or removing the version

Me, too. I will need to resolve a conflict, so I can remove it if you like.

@bartonjs
Copy link
Member

Eh, if you're indifferent and I'm indifferent, let's leave it, so I can have a "smaller than the whole world" experience :)

@stephentoub stephentoub merged commit 199580b into dotnet:main Jul 13, 2022
@stephentoub stephentoub deleted the moresafehandlefinalization branch July 13, 2022 11:09
@ghost ghost locked as resolved and limited conversation to collaborators Aug 12, 2022
@AndyAyersMS
Copy link
Member

Possible regression: dotnet/perf-autofiling-issues#6973

@bartonjs
Copy link
Member

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.

@stephentoub
Copy link
Member Author

stephentoub commented Aug 18, 2022

@bartonjs, I did touch this:
https://github.com/dotnet/runtime/pull/71991/files#diff-b5e1d8d2d47fbe472dd7de0d3d5450bc58de31363bee3ae108cf295bf879b4e4R44
I guess that could have caused this (an extra non-inlineable function call, an extra generic dictionary lookup, etc.)?

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?

@bartonjs
Copy link
Member

@stephentoub
Copy link
Member Author

In that case, yeah, I'm not on the hook :)

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.

6 participants