Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Warn in verbose mode on defining a finalizer that captures the object #2264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2954,6 +2954,42 @@ should_be_finalizable(VALUE obj)
rb_check_frozen(obj);
}

struct should_not_capture_data {
VALUE obj;
VALUE set;
bool found;
};

static void
should_not_capture_callback(const VALUE child, struct should_not_capture_data *data)
{
if (child == data->obj)
data->found = true;

if (data->found)
return;

// Maintain a set of objects already searched, so that we don't follow a cycle
VALUE key = rb_obj_id(child);
if (rb_hash_has_key(data->set, key))
return;
rb_hash_aset(data->set, key, Qtrue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just use the object itself as the hash key?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we use rb_objspace_reachable_objects_from we get internal objects - RUBY_T_IMEMO and I think specifically instruction sequences. These aren't hashable (don't respond to hash). I'd welcome a suggestion about alternative data structures to use instead of a Hash - could I use the st hash structure directly? Or some kind of identity hash?


rb_objspace_reachable_objects_from(child, (void (*)(unsigned long, void *)) &should_not_capture_callback, (void *)data);
}

static void
should_not_capture(VALUE block, VALUE obj)
{
struct should_not_capture_data data;
data.obj = obj;
data.set = rb_hash_new();
data.found = false;
rb_objspace_reachable_objects_from(block, (void (*)(unsigned long, void *)) &should_not_capture_callback, (void *)&data);
if (data.found)
rb_warn("object is reachable from finalizer - it may never be run");
}

/*
* call-seq:
* ObjectSpace.define_finalizer(obj, aProc=proc())
Expand All @@ -2963,6 +2999,10 @@ should_be_finalizable(VALUE obj)
* as an argument to <i>aProc</i>. If <i>aProc</i> is a lambda or
* method, make sure it can be called with a single argument.
*
* In verbose mode (<code>-w</code>) a warning will be issued if
* the object is reachable from <i>aProc</i>, which may prevent
* finalization.
*
*/

static VALUE
Expand All @@ -2979,6 +3019,9 @@ define_final(int argc, VALUE *argv, VALUE os)
should_be_callable(block);
}

if (ruby_verbose)
should_not_capture(block, obj);

return define_final0(obj, block);
}

Expand Down
26 changes: 26 additions & 0 deletions spec/ruby/core/objectspace/define_finalizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,30 @@ def scoped

ruby_exe(code).lines.sort.should == ["finalized1\n", "finalized2\n"]
end

it "warns in verbose mode if it is self-referencing" do
code = <<-RUBY
obj = "Test"
handler = Proc.new { puts "finalized" }
ObjectSpace.define_finalizer(obj, handler)
exit 0
RUBY

ruby_exe(code, :options => "-w", :args => "2>&1").should include("warning: object is reachable from finalizer - it may never be run")
end

it "warns in verbose mode if it is indirectly self-referencing" do
code = <<-RUBY
def scoped(indirect)
Proc.new { puts "finalized" }
end
obj = "Test"
indirect = [obj]
handler = scoped(indirect)
ObjectSpace.define_finalizer(obj, handler)
exit 0
RUBY

ruby_exe(code, :options => "-w", :args => "2>&1").should include("warning: object is reachable from finalizer - it may never be run")
end
end