Skip to content

Commit 2b610bf

Browse files
committed
Run "OnThreadExiting" during thread finalization. Testing with Mono has already hardened the cases that this makes weird, so we can reliably do it with CoreCLR
1 parent 398d964 commit 2b610bf

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

src/coreclr/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,18 @@ private void Initialize()
176176
private static partial void Initialize(ObjectHandleOnStack thread);
177177

178178
/// <summary>Clean up the thread when it goes away.</summary>
179-
~Thread() => InternalFinalize(); // Delegate to the unmanaged portion.
179+
~Thread()
180+
{
181+
// During the actual thread shutdown,
182+
// it may not be practical for us to run enough managed code to clean up
183+
// any managed code that needs to know when a thread exits.
184+
// Instead, run that clean up here when the thread object is finalized
185+
// (which is definitely after the thread has exited)
186+
OnThreadExiting();
187+
188+
// Do any additional finalization that's required on the unmanaged side.
189+
InternalFinalize();
190+
}
180191

181192
[MethodImpl(MethodImplOptions.InternalCall)]
182193
private extern void InternalFinalize();

src/coreclr/vm/corelib.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,6 @@ DEFINE_CLASS(DIRECTONTHREADLOCALDATA, Threading, Thread+DirectOnThreadLocalData)
969969
DEFINE_CLASS(THREAD, Threading, Thread)
970970
DEFINE_METHOD(THREAD, START_CALLBACK, StartCallback, IM_RetVoid)
971971
DEFINE_METHOD(THREAD, POLLGC, PollGC, NoSig)
972-
DEFINE_METHOD(THREAD, ON_THREAD_EXITING, OnThreadExiting, IM_RetVoid)
973972

974973
#ifdef FEATURE_OBJCMARSHAL
975974
DEFINE_CLASS(AUTORELEASEPOOL, Threading, AutoreleasePool)

src/coreclr/vm/threads.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,15 +2727,6 @@ void Thread::CooperativeCleanup()
27272727

27282728
GCX_COOP();
27292729

2730-
if (!IsGCSpecial())
2731-
{
2732-
// Allow managed subsystems to clean up on thread exit.
2733-
PREPARE_NONVIRTUAL_CALLSITE(METHOD__THREAD__ON_THREAD_EXITING);
2734-
DECLARE_ARGHOLDER_ARRAY(args, 1);
2735-
args[ARGNUM_0] = OBJECTREF_TO_ARGHOLDER(GetExposedObject());
2736-
CALL_MANAGED_METHOD_NORET(args);
2737-
}
2738-
27392730
// Clear any outstanding stale EH state that maybe still active on the thread.
27402731
#ifdef FEATURE_EH_FUNCLETS
27412732
ExInfo::PopTrackers((void*)-1);

0 commit comments

Comments
 (0)