Bug #4168: WeakRef is unsafe to use in Ruby 1.9
http://redmine.ruby-lang.org/issues/show/4168

Author: Brian Durand
Status: Open, Priority: Normal
Category: lib, Target version: 1.9.x
ruby -v: ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]

I've found a couple issues with the implementation of WeakRef in Ruby 1.9=
.

1. WeakRef is unsafe to use because the ObjectSpace will recycle object i=
ds for use with newly allocated objects after the old objects have been g=
arbage collected. This problem was not present in 1.8, but with the 1.9 t=
hreading improvements there is no guarantee that the garbage collector th=
read has finished running the finalizers before new objects are allocated=
. This can result in a WeakRef returning a different object than the one =
it originally referenced.

2. In Ruby 1.9 a Mutex is used to synchronize access to the shared data s=
tructures. However, Mutexes are not reentrant and the finalizers are sile=
ntly throwing deadlock errors if they run while new WeakRefs are being cr=
eated.

I've included in my patch a reimplementation of WeakRef called WeakRefere=
nce that does not extend Delegator. This code is based on the original We=
akRef code but has been rewritten so the variable names are a little clea=
rer. It replaces the Mutex with a Monitor and adds an additional check to=
 make sure that the object returned by the reference is the same one it o=
riginally referred to. WeakRef is rewritten as a Delegator wrapper around=
 a WeakReference for backward compatibility.

I went this route because in some Ruby implementations (like Ruby 1.8), D=
elegator is very heavy weight to instantiate and creating a collection of=
 thousands of WeakRefs will be slow and use up far too much memory (in fa=
ct, it would be nice to either backport this patch to Ruby 1.8 or the del=
egate changes from 1.9 to 1.8 if possible). I also don't really see the v=
alue of having weak references be delegators since is very unsafe to do a=
nything without wrapping the call in a "rescue RefError" block. The objec=
t can be reclaimed at any time so the only safe method to be sure you sti=
ll have it is to create a strong reference to the object at which point y=
ou might as well use that reference instead of the delegated methods on t=
he WeakRef. It also should be simpler for other implementations of Ruby l=
ike Jruby or Rubinius to map native weak references to a simpler interfac=
e.

Sample code with WeakReference

    orig =3D Object.new
    ref =3D WeakReference.new(orig)
    # ...
    obj =3D ref.object
    if obj
      # Do something
    end
    =

I also have a version of the patch which just fixes WeakRef to work witho=
ut introducing a new class, but I feel this version is the right way to g=
o.

Also included are unit tests for weak references but the test that checks=
 for the broken functionality is not 100% reliable since garbage collecti=
on is not deterministic. I've also included a script that shows the probl=
em in more detail with the existing weak reference implementation.

    ruby show_bug.rb 100000     # Run 100,000 iterations on the current i=
mplementation of WeakRef and report any problems
    ruby -I. show_bug.rb 100000 # Run 100,000 iterations on the new imple=
mentation of WeakRef and report any problems


----------------------------------------
http://redmine.ruby-lang.org
cmVxdWlyZSAnbW9uaXRvcicKCiMgV2Vha1JlZmVyZW5jZSBpcyBhIGNsYXNz
IHRvIHJlcHJlc2VudCBhIHJlZmVyZW5jZSB0byBhbiBvYmplY3QgdGhhdCBp
cyBub3Qgc2VlbiBieQojIHRoZSB0cmFjaW5nIHBoYXNlIG9mIHRoZSBnYXJi
YWdlIGNvbGxlY3Rvci4gIFRoaXMgYWxsb3dzIHRoZSByZWZlcmVuY2VkCiMg
b2JqZWN0IHRvIGJlIGdhcmJhZ2UgY29sbGVjdGVkIGFzIGlmIG5vdGhpbmcg
aXMgcmVmZXJyaW5nIHRvIGl0LgojCiMgVGhlIGRpZmZlcmVuY2UgYmV0d2Vl
biB0aGlzIGNsYXNzIGFuZCBXZWFrUmVmIGlzIHRoYXQgdGhpcyBjbGFzcyBk
b2VzIG5vdAojIHVzZSB0aGUgZGVsZWdhdG9yIHBhdHRlcm4gYW5kIHNvIGhh
cyBhbiBpbnRlcmZhY2UgbW9yZSBzdWl0ZWQgZm9yIGRldGVjdGluZwojIGlm
IHRoZSByZWZlcmVuY2VkIG9iamVjdCBoYXMgYmVlbiByZWNsYWltZWQuCiMK
IyBVc2FnZToKIwojICAgZm9vID0gT2JqZWN0Lm5ldwojICAgcmVmID0gQWN0
aXZlU3VwcG9ydDo6V2Vha1JlZmVyZW5jZS5uZXcoZm9vKQojICAgcmVmLmFs
aXZlPwkJCSMgc2hvdWxkIGJlIHRydWUKIyAgIHJlZi5vYmplY3QJCQkjIHNo
b3VsZCBiZSBmb28KIyAgIE9iamVjdFNwYWNlLmdhcmJhZ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