Skip to content

Commit c15cb70

Browse files
Match CoreCLR behaviour on thread start failure (#44124)
Co-authored-by: Aleksey Kliger (λgeek) <akliger@gmail.com>
1 parent 6d07222 commit c15cb70

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

src/mono/mono/metadata/threads.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,29 @@ start_wrapper (gpointer data)
13211321
g_assert_not_reached ();
13221322
}
13231323

1324+
static void
1325+
throw_thread_start_exception (guint32 error_code, MonoError *error)
1326+
{
1327+
ERROR_DECL (method_error);
1328+
1329+
MONO_STATIC_POINTER_INIT (MonoMethod, throw)
1330+
1331+
throw = mono_class_get_method_from_name_checked (mono_defaults.thread_class, "ThrowThreadStartException", 1, 0, method_error);
1332+
mono_error_assert_ok (method_error);
1333+
1334+
MONO_STATIC_POINTER_INIT_END (MonoMethod, throw)
1335+
g_assert (throw);
1336+
1337+
char *msg = g_strdup_printf ("0x%x", error_code);
1338+
MonoException *ex = mono_get_exception_execution_engine (msg);
1339+
g_free (msg);
1340+
1341+
gpointer args [1];
1342+
args [0] = ex;
1343+
1344+
mono_runtime_invoke_checked (throw, NULL, args, error);
1345+
}
1346+
13241347
/*
13251348
* create_thread:
13261349
*
@@ -1403,7 +1426,12 @@ create_thread (MonoThread *thread, MonoInternalThread *internal, MonoObject *sta
14031426
mono_threads_lock ();
14041427
mono_g_hash_table_remove (threads_starting_up, thread);
14051428
mono_threads_unlock ();
1429+
1430+
#ifdef ENABLE_NETCORE
1431+
throw_thread_start_exception (mono_w32error_get_last(), error);
1432+
#else
14061433
mono_error_set_execution_engine (error, "Couldn't create thread. Error 0x%x", mono_w32error_get_last());
1434+
#endif
14071435
/* ref is not going to be decremented in start_wrapper_internal */
14081436
mono_atomic_dec_i32 (&start_info->ref);
14091437
ret = FALSE;

src/mono/netcore/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,10 +692,5 @@
692692
<!-- marshal-ilgen.c:emit_invoke_call -->
693693
<method signature="System.Void .ctor()" />
694694
</type>
695-
696-
<!-- Used by binary formatter tests -->
697-
<type fullname="System.Threading.ThreadStartException">
698-
<method name=".ctor" />
699-
</type>
700695
</assembly>
701696
</linker>

src/mono/netcore/System.Private.CoreLib/src/System/Threading/Thread.Mono.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,11 @@ internal void StartCallback()
289289
}
290290
}
291291

292+
// Called from the runtime
293+
internal static void ThrowThreadStartException(Exception ex) => throw new ThreadStartException(ex);
294+
292295
[DynamicDependency(nameof(StartCallback))]
296+
[DynamicDependency(nameof(ThrowThreadStartException))]
293297
[MethodImplAttribute(MethodImplOptions.InternalCall)]
294298
private static extern void StartInternal(Thread runtime_thread);
295299
#endif

0 commit comments

Comments
 (0)