This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Sagiv.Malihi
Recipients Sagiv.Malihi
Date 2011-08-02.08:16:08
SpamBayes Score 7.188139e-12
Marked as misclassified No
Message-id <1312272969.55.0.673755441724.issue12680@psf.upfronthosting.co.za>
In-reply-to
Content
When trying to cPickle.loads() from several threads at once, there is a race condition when threads try to import modules.

An example will explain it best:
suppose I have module foo.py which takes some time to load:

import time
class A(object):
    def __setstate__(self, state):
        self.x = x
time.sleep(1)
x = 5

and a pickled version of an A() object stored in 'A.pkl'.
the following code, when run for the first time, will raise a NameError about 'x':

>>> p = open('A.pkl','rb').read()
>>> [thread.start_new(cPickle.loads, (p,)) for x in xrange(2)]

Unhandled exception in thread started by <built-in function loads>
Traceback (most recent call last):
  File "foo.py", line 7, in __setstate__
    self.x = x
NameError: global name 'x' is not defined


since the module is now loaded, subsequent calls to cPickle.loads will work as expected.

This was tested on 2.5.2, 2.7.1, and 3.2 on Ubuntu and on Windows 7.

please note that this bug was discovered when unpickling the standard 'decimal.Decimal' class (decimal.py is quite long and takes some time to import), and this is not some corner case.
History
Date User Action Args
2011-08-02 08:16:09Sagiv.Malihisetrecipients: + Sagiv.Malihi
2011-08-02 08:16:09Sagiv.Malihisetmessageid: <1312272969.55.0.673755441724.issue12680@psf.upfronthosting.co.za>
2011-08-02 08:16:08Sagiv.Malihilinkissue12680 messages
2011-08-02 08:16:08Sagiv.Malihicreate