Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
diff --git a/gc.c b/gc.c
index 63869a0..a698792 100644
--- a/gc.c
+++ b/gc.c
@@ -1499,11 +1499,16 @@ rb_objspace_call_finalizer(rb_objspace_t *objspace)
RVALUE *p, *pend;
RVALUE *final_list = 0;
size_t i;
+ rb_thread_t *th = GET_THREAD();
rest_sweep(objspace);
if (ATOMIC_EXCHANGE(finalizing, 1)) return;
+ /* avoid unforced finalizing reachable objects from VM */
+ /* The VM destructor needs these objects. */
+ th->vm->self ? rb_gc_mark(th->vm->self) : rb_vm_mark(th->vm);
+
/* run finalizers */
do {
finalize_deferred(objspace);
diff --git a/test/ruby/test_gc.rb b/test/ruby/test_gc.rb
index 9d68c1a..11d2b4c 100644
--- a/test/ruby/test_gc.rb
+++ b/test/ruby/test_gc.rb
@@ -134,4 +134,10 @@ class TestGc < Test::Unit::TestCase
ensure
GC::Profiler.disable
end
+
+ def test_finalizing_main_thread
+ assert_in_out_err(%w[--disable-gems], <<-EOS, ["\"finalize\""], [], "[ruby-dev:46647]")
+ ObjectSpace.define_finalizer(Thread.main) { p 'finalize' }
+ EOS
+ end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.