Skip to content

Commit 4a00c50

Browse files
monojenkinsluhenry
authored andcommitted
[2018-02] Harden JIT job control against cctors deadlocking. (#8235)
* [mini] Don't wait for other threads to finish JITing if the current thread is running a cctor. This eliminates a possible source of deadlocks found during AOT runs of Roslyn. * [mini] Introduce a 1s timeout to JIT job control to ensure we don't simply deadlock in case of execution dependency bugs. * [mini] fix spelling5DD
1 parent 34b31f0 commit 4a00c50

3 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎mono/metadata/threads-types.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,7 @@ mono_thread_internal_describe (MonoInternalThread *internal, GString *str);
275275
gboolean
276276
mono_thread_internal_is_current (MonoInternalThread *internal);
277277

278+
gboolean
279+
mono_threads_is_current_thread_in_protected_block (void);
280+
278281
#endif /* _MONO_METADATA_THREADS_TYPES_H_ */

‎mono/metadata/threads.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ mono_thread_get_abort_prot_block_count (MonoInternalThread *thread)
253253
return (state & ABORT_PROT_BLOCK_MASK) >> ABORT_PROT_BLOCK_SHIFT;
254254
}
255255

256+
gboolean
257+
mono_threads_is_current_thread_in_protected_block (void)
258+
{
259+
MonoInternalThread *thread = mono_thread_internal_current ();
260+
261+
return mono_thread_get_abort_prot_block_count (thread) > 0;
262+
}
263+
256264
void
257265
mono_threads_begin_abort_protected_block (void)
258266
{

‎mono/mini/mini-runtime.c‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,8 +1832,15 @@ typedef struct {
18321832
MonoCoopMutex lock;
18331833
} JitCompilationData;
18341834

1835+
/*
1836+
Timeout, in millisecounds, that we wait other threads to finish JITing.
1837+
This value can't be too small or we won't see enough methods being reused and it can't be too big to cause massive stalls due to unforseable circunstances.
1838+
*/
1839+
#define MAX_JIT_TIMEOUT_MS 1000
1840+
1841+
18351842
static JitCompilationData compilation_data;
1836-
static int jit_methods_waited, jit_methods_multiple, jit_methods_overload, jit_spurious_wakeups;
1843+
static int jit_methods_waited, jit_methods_multiple, jit_methods_overload, jit_spurious_wakeups_or_timeouts;
18371844

18381845
static void
18391846
mini_jit_init_job_control (void)
@@ -1898,7 +1905,7 @@ wait_or_register_method_to_compile (MonoMethod *method, MonoDomain *domain)
18981905
mono_counters_register ("JIT compile waited others", MONO_COUNTER_INT|MONO_COUNTER_JIT, &jit_methods_waited);
18991906
mono_counters_register ("JIT compile 1+ jobs", MONO_COUNTER_INT|MONO_COUNTER_JIT, &jit_methods_multiple);
19001907
mono_counters_register ("JIT compile overload wait", MONO_COUNTER_INT|MONO_COUNTER_JIT, &jit_methods_overload);
1901-
mono_counters_register ("JIT compile spurious wakeups", MONO_COUNTER_INT|MONO_COUNTER_JIT, &jit_spurious_wakeups);
1908+
mono_counters_register ("JIT compile spurious wakeups or timeouts", MONO_COUNTER_INT|MONO_COUNTER_JIT, &jit_spurious_wakeups_or_timeouts);
19021909
inited = TRUE;
19031910
}
19041911

@@ -1915,9 +1922,14 @@ wait_or_register_method_to_compile (MonoMethod *method, MonoDomain *domain)
19151922

19161923
unlock_compilation_data ();
19171924
return FALSE;
1918-
} else if (jit_tls->active_jit_methods > 0) {
1925+
} else if (jit_tls->active_jit_methods > 0 || mono_threads_is_current_thread_in_protected_block ()) {
19191926
//We can't suspend the current thread if it's already JITing a method.
19201927
//Dependency management is too compilated and we want to get rid of this anyways.
1928+
1929+
//We can't suspend the current thread if it's running a protected block (such as a cctor)
1930+
//We can't rely only on JIT nesting as cctor's can be run from outside the JIT.
1931+
1932+
//Finally, he hit a timeout or spurious wakeup. We're better off just giving up and keep recompiling
19211933
++entry->compilation_count;
19221934
++jit_methods_multiple;
19231935
++jit_tls->active_jit_methods;
@@ -1937,15 +1949,25 @@ wait_or_register_method_to_compile (MonoMethod *method, MonoDomain *domain)
19371949
++entry->threads_waiting;
19381950

19391951
g_assert (entry->has_cond);
1940-
mono_coop_cond_wait (&entry->cond, &compilation_data.lock);
1952+
mono_coop_cond_timedwait (&entry->cond, &compilation_data.lock, MAX_JIT_TIMEOUT_MS);
19411953
--entry->threads_waiting;
19421954

19431955
if (entry->done) {
19441956
unref_jit_entry (entry);
19451957
unlock_compilation_data ();
19461958
return TRUE;
19471959
} else {
1948-
++jit_spurious_wakeups;
1960+
//We hit the timeout or a spurious wakeup, fallback to JITing
1961+
g_assert (entry->ref_count > 1);
1962+
unref_jit_entry (entry);
1963+
++jit_spurious_wakeups_or_timeouts;
1964+
1965+
++entry->compilation_count;
1966+
++jit_methods_multiple;
1967+
++jit_tls->active_jit_methods;
1968+
1969+
unlock_compilation_data ();
1970+
return FALSE;
19491971
}
19501972
}
19511973
}

0 commit comments

Comments
 (0)