Skip to content

Better testing for finalization #935

@headius

Description

@headius

While attempting to write a spec for jruby/jruby#7267 I ran into various issues and questions...

  1. There are no specs testing that GC eventually finalizes objects. This is obviously difficult to predict, but it is behavior I believe we should be testing one way or another.
  2. I was testing that exceptions in one finalizer are not seen by the next finalizer, but could not figure out a way to eliminate the warning output from Ruby indicating that a finalizer raised an exception.

The spec I attempted is below, but only makes a best attempt at forcing GC-oriented finalization and still does not suppress the error output.

  it "hides raised exceptions from one finalizer to the next" do
    def scoped(result)
        Proc.new { result << "ok" if $!.nil?; raise }
    end
    def test(result)
      obj = "Test"
      # finalizer order may vary so both handlers check $! and raise an error
      ObjectSpace.define_finalizer(obj, scoped(result))
      ObjectSpace.define_finalizer(obj, scoped(result))
    end

    result = []
    begin
      old_verbose, $VERBOSE = $VERBOSE, false
      test(result)
    ensure
      $VERBOSE = old_verbose
    end

    100.times { GC.start; break if result.size == 2 }

    result.should == ["ok", "ok"]
  end

I don't want to leave the fix for jruby/jruby#7267 untested, but I'm unsure how we should move forward to improve the GC-triggered finalization specs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions