Skip to content

Commit

Permalink
fixing an assert (dotnet#106752)
Browse files Browse the repository at this point in the history
we are hitting this assert in gc_thread_function

assert ((n_heaps <= heap_number) || !gc_t_join.joined());

during the init stage. this is because the main thread (that does the GC init) is calling change_heap_count which acts like h0's thread while h0's thread can be anywhere before waiting on ee_suspend_event. so we could be in this situation -

1) main thread calls GCHeap::Init and creates h0's GC thread
2) main thread calls gc_heap::change_heap_count and this will change n_heaps to 1, and after there's another join, 
when the main thread and all other heaps' GC threads have joined, it changes gc_t_join.joined_p to true
3) h0's thread runs and sees that n_heaps is 1, and gc_t_join.joined_p is true which triggers the assert.

so this only happens during the init stage on h0's thread.
  • Loading branch information
Maoni0 authored Aug 22, 2024
1 parent 2af0051 commit 10107d3
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/coreclr/gc/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6999,8 +6999,21 @@ void gc_heap::gc_thread_function ()

while (1)
{
// inactive GC threads may observe gc_t_join.joined() being true here
assert ((n_heaps <= heap_number) || !gc_t_join.joined());
#ifdef DYNAMIC_HEAP_COUNT
if (gc_heap::dynamic_adaptation_mode == dynamic_adaptation_to_application_sizes)
{
// Inactive GC threads may observe gc_t_join.joined() being true here.
// Before the 1st GC happens, h0's GC thread can also observe gc_t_join.joined() being true because it's
// also inactive as the main thread (that inits the GC) will act as h0 (to call change_heap_count).
assert (((heap_number == 0) && (VolatileLoadWithoutBarrier (&settings.gc_index) == 0)) ||
(n_heaps <= heap_number) ||
!gc_t_join.joined());
}
else
#endif //DYNAMIC_HEAP_COUNT
{
assert (!gc_t_join.joined());
}

if (heap_number == 0)
{
Expand Down

0 comments on commit 10107d3

Please # to comment.