Repository navigation
Suppress finalizeation of ThreadMethodEntry class. Fix #10361. - #10460
Conversation
2699855 to
7525bed
Compare
lonitra
left a comment
There was a problem hiding this comment.
Thank you for your patience! Added some small comments
7525bed to
85b59d1
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #10460 +/- ##
===================================================
+ Coverage 72.61924% 72.62188% +0.00263%
===================================================
Files 2931 2931
Lines 620286 620280 -6
Branches 46942 46941 -1
===================================================
+ Hits 450447 450459 +12
+ Misses 161478 161461 -17
+ Partials 8361 8360 -1
Flags with carried forward coverage won't be shown. Click here to find out more. |
85b59d1 to
380fd2e
Compare
JeremyKuhne
left a comment
There was a problem hiding this comment.
In general this looks good, just a few questions / comments.
| Assert.Throws<TargetParameterCountException>(() => control.Invoke(method, Array.Empty<object>())); | ||
| } | ||
|
|
||
| #if DEBUG |
There was a problem hiding this comment.
I'm confused as to why this is DEBUG only. The comment seems to tell me that these tests will Debug.Fail.
There was a problem hiding this comment.
I'm confused as to why this is DEBUG only. The comment seems to tell me that these tests will Debug.Fail.
Yes, so these tests will never fail in release mode. And I put #if DEBUG to indicate that test actual only in debug build - am I wrong?
There was a problem hiding this comment.
@kirsan31 There is probably at least some value in running the tests in release on the slim chance that they fail. If we're concerned about test performance I'd rather see these conditionalized in code.
Also I have noticed some interesting thing. In protected override void Dispose(bool explicitDisposing)
{
base.Dispose(explicitDisposing);
if (explicitDisposing)
{
// The owner (ThreadMethodEntry) need to free only this waitHandle.
// But ThreadMethodEntry can't be Disposed directly because it exposed though IAsyncResult interface.
// So after freeing this waitHandle we mark owner ThreadMethodEntry to suppress finalization.
GC.SuppressFinalize(_owner);
}
}and ~ThreadMethodEntry()
{
_resetEvent?.Close();
Debug.Fail($"{nameof(ThreadMethodEntry)} finalization hit!");
}Due to public virtual void Close() => Dispose();
protected virtual void Dispose(bool explicitDisposing)
{
_waitHandle?.Close();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}could this be a problem? |
Presumably it is still rooted in a local? At least historically I remember the GC keeping all locals rooted to the end of a stack frame in debug. |
| { | ||
| // If something new is added here, it is necessary to reconsider the conditions of finalization. | ||
| // Look GC.SuppressFinalize(this); in constructor and GC.ReRegisterForFinalize(this); in AsyncWaitHandle. | ||
| _resetEvent?.Close(); |
There was a problem hiding this comment.
@kirsan31 I hadn't really internalized that we're touching a class in the finalizer. Why do we even have a finalizer? The event has its own SafeHandle that should take care of things.
There was a problem hiding this comment.
Ha, I think you're right - why did I decide to overcomplicate everything? 🤦♂️🤦♂️🤦♂️ Indeed, removing the destructor and disposing WaitHandle after Invoke should solve all the problems...
I'll correct...
5a8d2b1 to
d8f34f4
Compare
…otnet#10460) * Fix dotnet#10361. * Suggestions implemented. * Suggestions implemented. Tests fixes. * ThreadMethodEntry finalizer removed. Tests removed.
Fixes #10361
After every call to
BeginInvoke/Invokeinstance ofThreadMethodEntrywill end up in Freachable queue.Proposed changes
- Register for finalization only when WH is allocated.- SuppressFinalize after WH disposed.ThreadMethodEntryfinalizer.WaitHandleduringInvokecall.Customer Impact
ThreadMethodEntrywill not end in Freachable queue - GC will be more faster - overall improve performance.Regression?
Risk
Minimal.
Test methodology
New UT added.I would like to add tests for the triggering of the finalizer, but unfortunately we cannot correctly handle exceptions in it :( That is, the only option is to add some kind of debug only test logic - is this necessary?
P.S. I have many test failure (not related to this PR) on my pc:

P.P.S. I have build error with VS 17.8.3:
Microsoft Reviewers: Open in CodeFlow