[Image-SIG] Image crop race condition

Rob Hinchliff rhinchliff at adgholdings.co.uk
Wed Dec 1 14:35:44 CET 2010


I am writing a python application that crops an image and processes it in a number of threads.
The lazy loading of the cropped image is causing me exceptions, I think because of this race condition in Image.py v1.1.7 starting at line 1682:

# --------------------------------------------------------------------
# Lazy operations

class _ImageCrop(Image):

    def __init__(self, im, box):

        Image.__init__(self)

        x0, y0, x1, y1 = box
        if x1 < x0:
            x1 = x0
        if y1 < y0:
            y1 = y0

        self.mode = im.mode
        self.size = x1-x0, y1-y0

        self.__crop = x0, y0, x1, y1

        self.im = im.im

    def load(self):

        # lazy evaluation!
        if self.__crop:
            self.im = self.im.crop(self.__crop)
            #self.__crop = None

        if self.im:
            return self.im.pixel_access(self.readonly)

        # FIXME: future versions should optimize crop/paste
        # sequences!

The problem is in 'load' at line 1707.
'self.__crop' is checked for 'None', and enters the block.
Another thread that has already entered the block sets self.__crop to None on line 1709.
The original thread then calls crop with a  None object, causing an exception.
I've fixed the problem in my local copy by commenting out line 1709.
I don't know the design reasons for setting '__crop' to None (free the memory?), but I think you should consider using double-checked locking pattern here if you wish to keep it.
Otherwise, thankyou for making this excellent library available for public consumption!

Best Regards,

Rob


This email and any files transmitted with it are intended only for the personal and confidential use of the designated recipient(s) or entity named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited and you are requested to notify the sender immediately by email and delete this email from your system. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction or as an official statement of ADG Holdings LLP or its affiliated companies. Email transmission cannot be guaranteed to be secure or free of error and comes with a risk that the email contains a virus, is not compatible with your electronic system or has been modified. ADG Holdings LLP does not represent the information contained in this email is complete or accurate and should not be relied upon as such. Therefore ADG Holdings LLP does not accept any liability for any direct or consequential loss arising from the use, or reliance on, this email or its contents. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/image-sig/attachments/20101201/4e6d63e3/attachment.html>


More information about the Image-SIG mailing list