Skip to content

ObjectSpace::WeakMap is double-weak in CRuby #8456

Description

@headius

It appears that at some point the contract of ObjectSpace::WeakMap changed so that both the values and the keys are weak. CRuby may have had this behavior all along by accident, due to their using object pointers for keys (in such a way that they won't be prevented from being collected.

The documentation for WeakMap in CRuby was changed in 2023 to indicate that all object references (keys and values) are weak. Previous documentation did not make that clear.

ruby/ruby@39c072d#diff-14642ca05e367c9201974b1641f91730c15bde48eebe09f9494f5b7ecb42b2fbR983

At least one location in Rails assumes this weak-keyed behavior and falls back on an object_id-based map when running on JRuby:

https://github.com/rails/rails/blob/bc979c5cf4e29e2aba95b7b13ca53e7a02fddee2/activesupport/lib/active_support/descendants_tracker.rb#L23-L33

This code is not in the same place in recent versions, but may have been relocated.

We should emulate the CRuby behavior with a weak-keyed, weak-valued implementation of WeakMap.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions