
12 Feb
2024
12 Feb
'24
11:28 a.m.
Issue #20253 has been updated by byroot (Jean Boussier). We just found yet another issue with @etienne: ```ruby
Proc.new { }.freeze.clone (irb):1:in `initialize_copy': can't modify frozen Proc: #<Proc:0x000000011e3a96b0 (irb):1> (FrozenError) from (irb):1:in `initialize_clone' from (irb):1:in `clone' from (irb):1:in `<main>'
This one was introduced in Ruby 3.3.0.
----------------------------------------
Bug #20253: `Proc.dup` and `Proc#clone` don't preserve finalizers
https://bugs.ruby-lang.org/issues/20253#change-106688
* 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/