From: "byroot (Jean Boussier) via ruby-core" Date: 2024-02-09T16:59:38+00:00 Subject: [ruby-core:116657] [Ruby master Bug#20253] `Proc.dup` and `Proc#clone` don't preserve finalizers Issue #20253 has been updated by byroot (Jean Boussier). While looking at this, it appears that instance variables are also not copied over in `Proc#dup`, but somehow are in `Proc#clone`: ```ruby def ivar_dup(obj) obj.instance_variable_set(:@foo, 1) p [:dup, obj.dup.instance_variable_get(:@foo)] p [:clone, obj.clone.instance_variable_get(:@foo)] end ivar_dup(Object.new) ivar_dup(Proc.new { }) ``` ``` [:dup, 1] [:clone, 1] [:dup, nil] [:clone, 1] ``` Which really raise the question of why Proc has a completely different codepath for duping/cloning, AFAIK no other core type does this. ---------------------------------------- Bug #20253: `Proc.dup` and `Proc#clone` don't preserve finalizers https://bugs.ruby-lang.org/issues/20253#change-106667 * Author: byroot (Jean Boussier) * Status: Open * Priority: Normal * Backport: 3.0: WONTFIX, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- While reviewing the fix for [Bug #20250] @peterzhu2118 pointed that `FL_FINALIZE` should probably also be cleared. However after some extra testing, it appears `Object#dup` and `Object#clone` do copy over the finalizer (which makes sense). But for some reason `Proc` has its own `dup/clone` implementation, which does copy the `FL_FINALIZE` flag, but doesn't copy the finalizer: Test script: ```ruby def fin(sym) ->(_) { p sym } end obj = Object.new ObjectSpace.define_finalizer(obj, fin(:obj)) obj.dup obj.clone proc = Proc.new { } ObjectSpace.define_finalizer(proc, fin(:proc)) proc.dup proc.clone ``` Expected output: ``` :proc :proc :proc :obj :obj :obj ``` Actual output: ``` :proc :obj :obj :obj ``` This discrepancy is present all the way back to Ruby 1.9. It's so niche I'm not sure it's worth a backport though... -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/