[New-bugs-announce] [issue11875] OrderedDict.__reduce__ not threadsafe

Mario Juric report at bugs.python.org
Tue Apr 19 10:28:24 CEST 2011


New submission from Mario Juric <mjuric at ias.edu>:

The implementation of OrderedDict.__reduce__() in Python 2.7.1 is not thread safe because of the following four lines:

        tmp = self.__map, self.__root
        del self.__map, self.__root
        inst_dict = vars(self).copy()
        self.__map, self.__root = tmp

If one thread is pickling an OrderedDict, while another accesses it, a race condition occurs if the accessing thread accesses the dict after self.__map and self.__root have been delated, and before they've been set again (above).

This leads to an extremely difficult bug to diagnose when using multiprocessing.Queue to exchange OrderedDicts between multiple processes (because Queue uses a separate feeder thread to do the pickling).

The fix seems relatively easy -- use:

        inst_dict = vars(self).copy()
        del inst_dict['_OrderedDict__map'], inst_dict['_OrderedDict__root']

instead of the above four lines.

PS: This issue+fix may also apply to Python 3.x, although I haven't tested it there.

----------
components: Library (Lib)
messages: 134023
nosy: mjuric
priority: normal
severity: normal
status: open
title: OrderedDict.__reduce__ not threadsafe
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11875>
_______________________________________


More information about the New-bugs-announce mailing list