Thread-safety of dict

Adam Olsen rhamph at gmail.com
Fri Jun 1 14:35:38 EDT 2007


On 6/1/07, "Martin v. Löwis" <martin at v.loewis.de> wrote:
> > So there you have it: if you're using a dict with custom classes (or
> > anything other than str) across multiple threads, and without locking
> > it, it's possible (though presumably extremely rare) for a lookup to
> > fail even through the key was there the entire time.
>
> That could be fixed by adding a generation counter to the dictionary,
> right? Then an adversary would have to arrange for the generation
> counter to roll over for lookdict to not notice that the
> dictionary was modified.

Yup.  Although it'd still be technically possible to roll over the
counter, it's much easier to say how insanely unlikely it is.
Incidentally, only resizing should increment the counter; it's not
necessary to restart lookups for other accesses (and would harm
performance, as well as producing infinite loops in __cmp__ methods
that read from their containing dict.)

It occurs to me now that getting the original ma_table back could do
worse than just a failed lookup: if the size is smaller than before it
would lead to memory corruption and segfaults.  That could be fixed
with with a before/after check of ma_mask.

And if you're *really* feeling paranoid you could add reference
counting to ma_table.  I doubt anybody cares quite that much though.
;)

-- 
Adam Olsen, aka Rhamphoryncus



More information about the Python-list mailing list