Mantis - Squeak
Viewing Issue Advanced Details
6955 Collections major always 02-26-08 14:30 02-26-08 14:30
0006955: Weak collections are not thread safe with respect to finalization
Weak collections (excluding WeakRegistry) may get finalized while being accessed (by a process with priority > userInterruptPriority) leading to a misrepresentation of whether they contain a particular element/key at any given moment.

For instance, with a WeakKeyDictionary, on doing #at:ifAbsentPut: may insert new entries with identical keys.

One option would be to expose the WeakArray FinalizationLock to process that modify such weak collections though this would effectively be a global lock across all weak collections.

As a better approach, weak collections should be protected in a similar manner to WeakRegistry for any operation that could change their underlying state.
The following code highlights the problem most of the time!

d := WeakIdentityKeyDictionary new.
WeakArray addWeakDependent: d.
oc := OrderedCollection new.
[1000 timesRepeat: [
    o := Morph new.
    2 atRandom > 1 ifTrue: [oc add: o].
    d at: o ifAbsentPut: ['something'].
    10 timesRepeat: [
        d at: o ifAbsent: [self error: 'duplicate ', o oopString].
        (Delay forMilliseconds: 1) wait]].
self inform: 'completed'] forkAt: Processor lowIOPriority

There are no notes attached to this issue.