Skip to content

Instantly share code, notes, and snippets.

@authorNari
Created November 28, 2012 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save authorNari/4160845 to your computer and use it in GitHub Desktop.
Save authorNari/4160845 to your computer and use it in GitHub Desktop.
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