Skip to content

Commit

Permalink
Disable GC even during finalizing
Browse files Browse the repository at this point in the history
We're seeing a crash during shutdown in rb_gc_impl_objspace_free because
it's running lazy sweeping during shutdown. It appears that it's due to
`finalizing` being set, which causes GC to not be aborted and not
disabled which causes it to be in lazy sweeping at shutdown.

The full stack trace is:

    #6  rb_bug (fmt=fmt@entry=0x5643b8ebde78 "lazy sweeping underway when freeing object space") at error.c:1095
    #7  0x00005643b8a3c697 in rb_gc_impl_objspace_free (objspace_ptr=<optimized out>) at gc/default.c:9507
    #8  0x00005643b8c269eb in ruby_vm_destruct (vm=0x7e2fdc84d000) at vm.c:3141
    #9  0x00005643b8a5147b in rb_ec_cleanup (ec=<optimized out>, ex=<optimized out>) at eval.c:263
    #10 0x00005643b8a51c93 in ruby_run_node (n=<optimized out>) at eval.c:319
    #11 0x00005643b8a4c7c7 in rb_main (argv=0x7fffef15e7f8, argc=18) at ./main.c:43
    #12 main (argc=<optimized out>, argv=<optimized out>) at ./main.c:62
  • Loading branch information
peterzhu2118 committed Aug 8, 2024
1 parent da8cf99 commit 868d63f
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion gc/default.c
Original file line number Diff line number Diff line change
Expand Up @@ -3233,11 +3233,17 @@ rb_gc_impl_shutdown_call_finalizer(void *objspace_ptr)
#if RGENGC_CHECK_MODE >= 2
gc_verify_internal_consistency(objspace);
#endif
if (RUBY_ATOMIC_EXCHANGE(finalizing, 1)) return;

/* prohibit incremental GC */
objspace->flags.dont_incremental = 1;

if (RUBY_ATOMIC_EXCHANGE(finalizing, 1)) {
/* Abort incremental marking and lazy sweeping to speed up shutdown. */
gc_abort(objspace);
dont_gc_on();
return;
}

/* force to run finalizer */
while (finalizer_table->num_entries) {
struct force_finalize_list *list = 0;
Expand Down

0 comments on commit 868d63f

Please sign in to comment.