Issue #22251 has been updated by Eregon (Benoit Daloze). Backport changed from 3.3: WONTFIX, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: WONTFIX, 3.4: WONTFIX, 4.0: WONTFIX I bisected it to https://github.com/ruby/ruby/commit/b2feb09efe. This seems big to backport, and I agree it's not so important, so marking all versions as `WONTFIX`. `#size` is just `st_table_size(w->table)` so what changed is that purging entries was lazy before that commit and eager after (`rb_wmap_handle_weak_references`). The behavior on master is better so I'll make a PR to add a CRuby test for it so we don't regress this unknowingly. ---------------------------------------- Bug #22251: ObjectSpace::WeakMap#size can return a stale value https://bugs.ruby-lang.org/issues/22251#change-118602 * Author: Eregon (Benoit Daloze) * Status: Closed * ruby -v: ruby 4.0.6 (2026-07-14 revision 03b6d3f889) +PRISM [arm64-darwin25] * Backport: 3.3: WONTFIX, 3.4: WONTFIX, 4.0: WONTFIX ---------------------------------------- ``` $ ruby -e 'GC.disable; m = ObjectSpace::WeakMap.new; Thread.new { m[Object.new] = Object.new }.join; GC.start; p m.size; p m.keys; p m.size' 1 [] 0 ``` The first line is 1 but I would expect 0. Interestingly calling `#keys` seems to force an update. If I GC twice then it seems correct: ``` $ ruby -e 'GC.disable; m = ObjectSpace::WeakMap.new; Thread.new { m[Object.new] = Object.new }.join; GC.start; GC.start; p m.size; p m.keys; p m.size' 0 [] 0 ``` Could we decrease size whenever we "remove" an entry? Apologies if that's already known, I searched for an existing issue for this but didn't find anything. -- https://bugs.ruby-lang.org/