Skip to content

Commit e301847

Browse files
authored
Abort async suspend request on failure doing preemptive suspend. (#15486)
When a preemptive suspend fails, state machine will get into an invalid state and the thread is in an unknown state (running/self-suspended). In case of failure in mono_threads_suspend_begin_async_suspend, a new method should be called, mono_threads_transition_abort_async_suspend making sure state machine reflects correct thread state as well as reporting correct result to initial caller of mono_threads_suspend_begin_async_suspend.
1 parent 08feb42 commit e301847

5 files changed

Lines changed: 90 additions & 3 deletions

File tree

‎mono/utils/mono-threads-mach.c‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,16 @@ mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interru
7373
} while (ret == KERN_ABORTED);
7474

7575
THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %d\n", (gpointer)(gsize)info->native_handle, ret);
76-
if (ret != KERN_SUCCESS)
76+
if (ret != KERN_SUCCESS) {
77+
if (!mono_threads_transition_abort_async_suspend (info)) {
78+
/* We raced with self suspend and lost so suspend can continue. */
79+
g_assert (mono_threads_is_hybrid_suspension_enabled ());
80+
info->suspend_can_continue = TRUE;
81+
THREADS_SUSPEND_DEBUG ("\tlost race with self suspend %p\n", (gpointer)(gsize)info->native_handle);
82+
return TRUE;
83+
}
7784
return FALSE;
85+
}
7886

7987
if (!mono_threads_transition_finish_async_suspend (info)) {
8088
/* We raced with self-suspend and lost. Resume the native

‎mono/utils/mono-threads-posix.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#endif
2525

2626
#include <mono/utils/mono-threads.h>
27+
#include <mono/utils/mono-threads-coop.h>
2728
#include <mono/utils/mono-coop-semaphore.h>
2829
#include <mono/metadata/gc-internals.h>
2930
#include <mono/utils/mono-threads-debug.h>
@@ -352,6 +353,13 @@ mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interru
352353
mono_threads_add_to_pending_operation_set (info);
353354
return TRUE;
354355
}
356+
if (!mono_threads_transition_abort_async_suspend (info)) {
357+
/* We raced with self suspend and lost so suspend can continue. */
358+
g_assert (mono_threads_is_hybrid_suspension_enabled ());
359+
info->suspend_can_continue = TRUE;
360+
THREADS_SUSPEND_DEBUG ("\tlost race with self suspend %p\n", mono_thread_info_get_tid (info));
361+
return TRUE;
362+
}
355363
return FALSE;
356364
}
357365

‎mono/utils/mono-threads-state-machine.c‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,59 @@ Don't expect these to be pulsed - they're not problematic.
567567
}
568568
}
569569

570+
/*
571+
Abort last step of preemptive suspend in case of failure to async suspend thread.
572+
This function makes sure state machine reflects current state of thread (running/suspended)
573+
in case of failure to complete async suspend of thread. NOTE, thread can still have reached
574+
a suspend state (in case of self-suspend).
575+
576+
Returns TRUE if async suspend request was successfully aborted. Thread should be in STATE_RUNNING.
577+
Returns FALSE if async suspend request was successfully aborted but thread already reached self-suspended.
578+
*/
579+
gboolean
580+
mono_threads_transition_abort_async_suspend (MonoThreadInfo* info)
581+
{
582+
int raw_state, cur_state, suspend_count;
583+
gboolean no_safepoints;
584+
585+
retry_state_change:
586+
UNWRAP_THREAD_STATE (raw_state, cur_state, suspend_count, no_safepoints, info);
587+
switch (cur_state) {
588+
case STATE_SELF_SUSPENDED: //async suspend raced with self suspend and lost
589+
case STATE_BLOCKING_SELF_SUSPENDED: //async suspend raced with blocking and lost
590+
if (no_safepoints)
591+
mono_fatal_with_history ("no_safepoints = TRUE, but should be FALSE");
592+
trace_state_change_sigsafe ("ABORT_ASYNC_SUSPEND", info, raw_state, cur_state, no_safepoints, 0, "");
593+
return FALSE; //thread successfully reached suspend state.
594+
case STATE_ASYNC_SUSPEND_REQUESTED:
595+
case STATE_BLOCKING_SUSPEND_REQUESTED:
596+
if (!(suspend_count > 0))
597+
mono_fatal_with_history ("suspend_count = %d, but should be > 0", suspend_count);
598+
if (no_safepoints)
599+
mono_fatal_with_history ("no_safepoints = TRUE, but should be FALSE");
600+
if (suspend_count > 1) {
601+
if (mono_atomic_cas_i32 (&info->thread_state, build_thread_state (cur_state, suspend_count - 1, no_safepoints), raw_state) != raw_state)
602+
goto retry_state_change;
603+
trace_state_change ("ABORT_ASYNC_SUSPEND", info, raw_state, cur_state, no_safepoints, -1);
604+
} else {
605+
if (mono_atomic_cas_i32 (&info->thread_state, build_thread_state (STATE_RUNNING, 0, no_safepoints), raw_state) != raw_state)
606+
goto retry_state_change;
607+
trace_state_change ("ABORT_ASYNC_SUSPEND", info, raw_state, STATE_RUNNING, no_safepoints, -1);
608+
}
609+
return TRUE; //aborting thread suspend request succeded, thread is running.
610+
611+
/*
612+
STATE_RUNNING: A thread cannot escape suspension once requested.
613+
STATE_ASYNC_SUSPENDED: There can be only one suspend initiator at a given time, meaning this state should have been visible on the first stage of suspend.
614+
STATE_BLOCKING: If a thread is subject to preemptive suspend, there is no race as the resume initiator should have suspended the thread to STATE_BLOCKING_ASYNC_SUSPENDED or STATE_BLOCKING_SELF_SUSPENDED before resuming.
615+
With cooperative suspend, there are no finish_async_suspend transitions since there's no path back from asyns_suspend requested to running.
616+
STATE_BLOCKING_ASYNC_SUSPENDED: There can only be one suspend initiator at a given time, meaning this state should have ben visible on the first stage of suspend.
617+
*/
618+
default:
619+
mono_fatal_with_history ("Cannot transition thread %p from %s with ABORT_ASYNC_SUSPEND", mono_thread_info_get_tid (info), state_name (cur_state));
620+
}
621+
}
622+
570623
/*
571624
This performs the last step of preemptive suspend.
572625

‎mono/utils/mono-threads-windows.c‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interru
182182
result = SuspendThread (handle);
183183
THREADS_SUSPEND_DEBUG ("SUSPEND %p -> %u\n", GUINT_TO_POINTER (id), result);
184184
if (result == (DWORD)-1) {
185+
if (!mono_threads_transition_abort_async_suspend (info)) {
186+
/* We raced with self suspend and lost so suspend can continue. */
187+
g_assert (mono_threads_is_hybrid_suspension_enabled ());
188+
info->suspend_can_continue = TRUE;
189+
THREADS_SUSPEND_DEBUG ("\tlost race with self suspend %p\n", mono_thread_info_get_tid (info));
190+
return TRUE;
191+
}
185192
THREADS_SUSPEND_DEBUG ("SUSPEND FAILED, id=%p, err=%u\n", GUINT_TO_POINTER (id), GetLastError ());
186193
return FALSE;
187194
}
@@ -195,9 +202,16 @@ mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interru
195202
CONTEXT context;
196203
context.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
197204
if (!GetThreadContext (handle, &context)) {
198-
THREADS_SUSPEND_DEBUG ("SUSPEND FAILED (GetThreadContext), id=%p, err=%u\n", GUINT_TO_POINTER (id), GetLastError ());
199205
result = ResumeThread (handle);
200206
g_assert (result == 1);
207+
if (!mono_threads_transition_abort_async_suspend (info)) {
208+
/* We raced with self suspend and lost so suspend can continue. */
209+
g_assert (mono_threads_is_hybrid_suspension_enabled ());
210+
info->suspend_can_continue = TRUE;
211+
THREADS_SUSPEND_DEBUG ("\tlost race with self suspend %p\n", mono_thread_info_get_tid (info));
212+
return TRUE;
213+
}
214+
THREADS_SUSPEND_DEBUG ("SUSPEND FAILED (GetThreadContext), id=%p, err=%u\n", GUINT_TO_POINTER (id), GetLastError ());
201215
return FALSE;
202216
}
203217

‎mono/utils/mono-threads.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,10 @@ This begins async suspend. This function must do the following:
563563
-Call mono_threads_transition_finish_async_suspend as part of its async suspend.
564564
-Register the thread for pending suspend with mono_threads_add_to_pending_operation_set if needed.
565565
566-
If begin suspend fails the thread must be left uninterrupted and resumed.
566+
If begin suspend fails the following should be done:
567+
-The thread must be left uninterrupted and resumed.
568+
-Call mono_threads_transition_abort_async_suspend to make sure thread has correct state.
569+
-No pending suspend should be registered with mono_threads_add_to_pending_operation_set for this operation.
567570
*/
568571
gboolean mono_threads_suspend_begin_async_suspend (THREAD_INFO_TYPE *info, gboolean interrupt_kernel);
569572

@@ -707,6 +710,7 @@ MonoRequestSuspendResult mono_threads_transition_request_suspension (THREAD_INFO
707710
MonoSelfSupendResult mono_threads_transition_state_poll (THREAD_INFO_TYPE *info);
708711
MonoResumeResult mono_threads_transition_request_resume (THREAD_INFO_TYPE* info);
709712
MonoPulseResult mono_threads_transition_request_pulse (THREAD_INFO_TYPE* info);
713+
gboolean mono_threads_transition_abort_async_suspend (THREAD_INFO_TYPE* info);
710714
gboolean mono_threads_transition_finish_async_suspend (THREAD_INFO_TYPE* info);
711715
MonoDoBlockingResult mono_threads_transition_do_blocking (THREAD_INFO_TYPE* info, const char* func);
712716
MonoDoneBlockingResult mono_threads_transition_done_blocking (THREAD_INFO_TYPE* info, const char* func);

0 commit comments

Comments
 (0)