[ZODB-Dev] Race condition in Connection.close

Marius Gedminas mgedmin at b4net.lt
Thu Apr 8 09:22:53 EDT 2004


On Wed, Apr 07, 2004 at 09:35:37PM +0300, Marius Gedminas wrote:
> I think I found a race condition in Connection.close in the current ZODB
> 3 CVS:
> 
>     # ZODB/Connection.py
>     ...
>     class Connection(...):
>         ...
>         def close(self):
>             ...
> 
>             # Return the connection to the pool.
>             if self._db is not None:
>                 self._db._closeConnection(self)             # 1
>                 self._db = None                             # 2
> 
> Assigning to self._db happens after self has been added to the
> connection pool.  If another thread gets scheduled between points 1 and
> 2, it might have already started using this connection.
[...]
> The line numbers might be slightly wrong since I added some debugging
> code to Connection.py, and hadn't updated to the very last CVS version
> before debugging this problem.  However the relevant part of
> Connection.close is not changed in the latest CVS.

I can reliably reproduce the error with the latest ZODB CVS in about 1
in 100 test runs.

> I'll try testing the obvious fix tomorrow and see if this fixes the
> errors I saw.
> 
>             # Return the connection to the pool.
>             if self._db is not None:
>                 db = self._db
>                 self._db = None
>                 db._closeConnection(self)

Running the tests with this change applied did not show any errors after
670 runs.

Should I commit the fix to ZODB CVS?  I've got no idea how to write a
unit test for it.  Well, maybe stub _closeConnection and check that the
connection that is passed has its _db attribute set to None.

Marius Gedminas
-- 
Hanlon's Razor:
        Never attribute to malice that which is adequately explained
        by stupidity.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zodb-dev/attachments/20040408/605ce5b6/attachment.bin


More information about the ZODB-Dev mailing list