Skip to content

Commit 00c38c7

Browse files
authored
Deadlock during rundown using interpreter. (#58996)
Identified deadlock between finalizer thread and rundown enumerating all interpreter method. Since rundown will query for method name in callback when iterating interpreter methods, interp_jit_info_foreach, that might lead to additional loader activity. The hash map in interpreter keeping the methods is locked with default JIT memory manager, but since the callback might end up in mono_class_create_from_typedef that will take loader lock, we get the following lock order on that code path, memory manager->loader lock. Finalizer thread invokes OnThreadExiting using interpreter and that might end up with a reverse lock order, loader lock->memory manager on that code path, so these two have a potential to deadlock. This is not a problem under JIT or AOT since the JIT hash table is lock free therefore not causing any deadlocks due to lock order between memory manager and loader lock. Could be fixed by changing into a lock free hash table in interpreters for interp_code_hash might be to risky at this point. A more safe fix is to take a copy of the pointers while holding lock and then iterate using local copy (simple array of pointers). Since this method is only called during rundown, only when using interpreter, and only include the pointers (InterpMethod *) we use with the callback, it will have some temporary memory impact (allocating an array of pointers), but will mitigate the deadlock since we can safely call iterator callback without holding the lock. It will also improve interpreter performance in situations where we run session rundown, since lock will be held a much shorter amount of time.
1 parent a7b0193 commit 00c38c7

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

src/mono/mono/mini/interp/interp.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7435,28 +7435,44 @@ interp_invalidate_transformed (void)
74357435
}
74367436

74377437
typedef struct {
7438-
InterpJitInfoFunc func;
7439-
gpointer user_data;
7440-
} InterpJitInfoFuncUserData;
7438+
MonoJitInfo **jit_info_array;
7439+
gint size;
7440+
gint next;
7441+
} InterpCopyJitInfoFuncUserData;
74417442

74427443
static void
7443-
interp_call_jit_info_func (gpointer imethod, gpointer user_data)
7444+
interp_copy_jit_info_func (gpointer imethod, gpointer user_data)
74447445
{
7445-
InterpJitInfoFuncUserData *data = (InterpJitInfoFuncUserData *)user_data;
7446-
data->func (((InterpMethod *)imethod)->jinfo, data->user_data);
7446+
InterpCopyJitInfoFuncUserData *data = (InterpCopyJitInfoFuncUserData*)user_data;
7447+
if (data->next < data->size)
7448+
data->jit_info_array [data->next++] = ((InterpMethod *)imethod)->jinfo;
74477449
}
74487450

74497451
static void
74507452
interp_jit_info_foreach (InterpJitInfoFunc func, gpointer user_data)
74517453
{
7452-
InterpJitInfoFuncUserData data = {func, user_data};
7454+
InterpCopyJitInfoFuncUserData copy_jit_info_data;
74537455

74547456
// FIXME: Enumerate all memory managers
74557457
MonoJitMemoryManager *jit_mm = get_default_jit_mm ();
74567458

7457-
jit_mm_lock (jit_mm);
7458-
mono_internal_hash_table_apply (&jit_mm->interp_code_hash, interp_call_jit_info_func, &data);
7459-
jit_mm_unlock (jit_mm);
7459+
// Can't keep memory manager lock while iterating and calling callback since it might take other locks
7460+
// causing poential deadlock situations. Instead, create copy of interpreter imethod jinfo pointers into
7461+
// plain array and use pointers from array when when running callbacks.
7462+
copy_jit_info_data.size = mono_atomic_load_i32 (&(jit_mm->interp_code_hash.num_entries));
7463+
copy_jit_info_data.next = 0;
7464+
copy_jit_info_data.jit_info_array = (MonoJitInfo**) g_new (MonoJitInfo*, copy_jit_info_data.size);
7465+
if (copy_jit_info_data.jit_info_array) {
7466+
jit_mm_lock (jit_mm);
7467+
mono_internal_hash_table_apply (&jit_mm->interp_code_hash, interp_copy_jit_info_func, &copy_jit_info_data);
7468+
jit_mm_unlock (jit_mm);
7469+
}
7470+
7471+
if (copy_jit_info_data.jit_info_array) {
7472+
for (size_t i = 0; i < copy_jit_info_data.next; ++i)
7473+
func (copy_jit_info_data.jit_info_array [i], user_data);
7474+
g_free (copy_jit_info_data.jit_info_array);
7475+
}
74607476
}
74617477

74627478
static void

0 commit comments

Comments
 (0)