Skip to content

Commit 2253a86

Browse files
committed
Strip default proc in Hash#freeze
A frozen Hash cannot be mutated, so a default proc that inserts missing keys would raise FrozenError anyway. Clearing it before freeze allows Hashes to be made Ractor-shareable (default procs capture self, which prevents Ractor.make_shareable from succeeding).
1 parent 1d5d5f5 commit 2253a86

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

hash.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ static VALUE rb_hash_s_try_convert(VALUE, VALUE);
103103
VALUE
104104
rb_hash_freeze(VALUE hash)
105105
{
106+
/* Clear the default proc before freezing. A frozen Hash cannot be
107+
* mutated, so a default proc that inserts missing keys would raise
108+
* FrozenError anyway. Removing it allows the Hash to be made
109+
* Ractor-shareable (default procs capture self, which prevents
110+
* Ractor.make_shareable from succeeding). */
111+
if (FL_TEST_RAW(hash, RHASH_PROC_DEFAULT)) {
112+
SET_DEFAULT(hash, Qnil);
113+
}
106114
return rb_obj_freeze(hash);
107115
}
108116

0 commit comments

Comments
 (0)