Bug 49274 - cctor + ThreadAbortException random failures
Summary: cctor + ThreadAbortException random failures
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-12-08 06:32 UTC by Zoltan Varga
Modified: 2017-10-06 23:30 UTC (History)
3 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:


Description Zoltan Varga 2016-12-08 06:32:12 UTC
This can be reproduced by the following testcase:
using System;
using System.Reflection;
using System.Threading;

public class Tests
	public static void Main (String[] args) {
		Timer t = new Timer (delegate (object state) {}, null, 10000, 1);

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'System.Collections.Generic.List`1' threw an exception. ---> System.Threading.ThreadAbortException
   --- End of inner exception stack trace ---
  at System.Threading.Timer+Scheduler.SchedulerThread () [0x0000f] in <dbd8d83e40d84547af35267a08f893f8>:0 

Applying the following patch makes it happen more often:
diff --git a/mono/metadata/threads.c b/mono/metadata/threads.c
index 6721ba5..3c8d5ce 100644
--- a/mono/metadata/threads.c
+++ b/mono/metadata/threads.c
@@ -854,6 +854,7 @@ static guint32 WINAPI start_wrapper_internal(StartInfo *start_info, gsize *stack
                g_assert (start_delegate != NULL);
+               mono_thread_info_sleep (3, NULL);
                /* we may want to handle the exception here. See comment below on unhandled exceptions */
                args [0] = (gpointer) start_delegate_arg;
                mono_runtime_delegate_invoke_checked (start_delegate, args, &error);

What happens is that by the time 		mono_threads_begin_abort_protected_block ();
is called in mono_runtime_class_init (), there is already a tae executing in async_abort_critical ().

Note You need to log in before you can comment on or make changes to this bug.