Project

General

Profile

Actions

Bug #19531

closed

ObjectSpace::WeakMap: replaced values still clear the key they were assigned to

Added by byroot (Jean Boussier) about 1 year ago. Updated 9 months ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:112906]

Description

Reproduction script

wmap = ObjectSpace::WeakMap.new

a = "A"
b = "B"

wmap[1] = a
wmap[1] = b # the table entry with 1 is still in the list of entries to clear when `a` is GCed

a = nil

GC.start

p wmap[1] # Should be `"B"`, but is `nil`

Explanation

What happens is that when we set wmap[1] = "A", WeakMap internally keeps a list of keys to clear when "A" is GCed, e.g. pseudo code:

class WeakMap
  def []=(key, value)
    @hash[key] = value
    @reverse[value] << key
  end
end

But it doesn't clear previously kept mapping when a key is overwritten.

I'll work on a fix.

References

https://github.com/protocolbuffers/protobuf/pull/12216

Actions #2

Updated by byroot (Jean Boussier) about 1 year ago

  • Status changed from Open to Closed

Applied in changeset git|3592b24cdc07ed89eecb39161f21fe721a89a5de.


ObjectSpace::WeakMap: clean inverse reference when an entry is re-assigned

[Bug #19531]

wmap[1] = "A"
wmap[1] = "B"

In the example above, we need to remove the "A" => 1 inverse reference
so that when "A" is GCed the 1 key isn't deleted.

Updated by nagachika (Tomoyuki Chikanaga) 9 months ago

  • Backport changed from 2.7: WONTFIX, 3.0: REQUIRED, 3.1: REQUIRED, 3.2: REQUIRED to 2.7: WONTFIX, 3.0: REQUIRED, 3.1: REQUIRED, 3.2: DONE

ruby_3_2 46b62f44ce30bf234a76114c8249081e47ce3da4 merged revision(s) 3592b24cdc07ed89eecb39161f21fe721a89a5de.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0