Subject: bug#10836: guardians and weak references

From: Mark H Weaver <[email-masked]>
Date: Tue, 28 Feb 2012 20:22:28 -0500

Andy Wingo <[email-masked]> writes:
> I tried reimplementing weak tables using finalizers.  (The finalizers
> remove elements from the table).  As long as we ensure that the
> guardian's finalizers run first, which is possible for Guile to do, that
> does fix this bug.
>
> However, using finalizers has the downside that it introduces races with
> custom hash/equality predicates.  Consider:
>
>   * Add an entry X -> Y to a weak-key table.
>
>   * X becomes unreachable.  Libgc enqueues a finalizer.
>
>   * Lookup the handle for (X -> Y), but using a custom predicate that
>     relies on some aspect of X that is not its identity.  Keep X
>     around.

Is there a need for weak-key tables that use something other than 'eq?'
as the predicate?  It's not clear to me that any other predicate makes
sense for a weak-key table.  To my mind, the idea is that we can safely
delete table entries if and only if we can prove that they could not be
looked up anyway.  This fits in very nicely with the concept of garbage
collection.

On the other hand, even for 'eq?' weak-key tables, there's another way
to retrieve entries without the key: the table iterators 'hash-fold' et
al.  I guess this introduces the same race for _any_ kind of hash table.

      Mark