From: Gregory P. Smith Date: Sun, 17 Aug 2008 23:06:19 +0000 (+0000) Subject: Backport of r64767 from trunk X-Git-Tag: v2.5.3c1~60 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7a076bed4c2a03e5fa0995228b99c80e9ad0bc03;p=python Backport of r64767 from trunk Fixes Issue #3309: Fix bz2.BZFile iterator to release its internal lock properly when raising an exception due to the bz2file being closed. Prevents a deadlock. --- diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py index 709850d229..554fe868af 100644 --- a/Lib/test/test_bz2.py +++ b/Lib/test/test_bz2.py @@ -110,6 +110,17 @@ class BZ2FileTest(BaseTest): self.assertEqual(list(iter(bz2f)), sio.readlines()) bz2f.close() + def testClosedIteratorDeadlock(self): + # "Test that iteration on a closed bz2file releases the lock." + # http://bugs.python.org/issue3309 + self.createTempFile() + bz2f = BZ2File(self.filename) + bz2f.close() + self.assertRaises(ValueError, bz2f.next) + # This call will deadlock of the above .next call failed to + # release the lock. + self.assertRaises(ValueError, bz2f.readlines) + def testXReadLines(self): # "Test BZ2File.xreadlines()" self.createTempFile() diff --git a/Misc/NEWS b/Misc/NEWS index 8864aa9849..23c2384eb6 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -148,6 +148,10 @@ Library up in the child process to prevent deadlock and report proper thread counts if the new process uses the threading module. +- Issue #3309: Fix bz2.BZFile iterator to release its internal lock + properly when raising an exception due to the bz2file being closed. + Prevents a deadlock. + Extension Modules ----------------- diff --git a/Modules/bz2module.c b/Modules/bz2module.c index 9e868c5eb6..73de628e86 100644 --- a/Modules/bz2module.c +++ b/Modules/bz2module.c @@ -1437,6 +1437,7 @@ BZ2File_iternext(BZ2FileObject *self) PyStringObject* ret; ACQUIRE_LOCK(self); if (self->mode == MODE_CLOSED) { + RELEASE_LOCK(self); PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); return NULL;