summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorNir Soffer <nirsof@gmail.com>2017-07-26 23:27:08 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2017-07-26 23:27:08 (GMT)
commit25de5baf3eaebddbf879aacf49c0f614f922dc42 (patch)
treec5622518171735ef088c48e22a821496ce1eba0e /Lib
parentbb7fd3f4d08091b2b68333267a4d5fdef03bda44 (diff)
downloadcpython-25de5baf3eaebddbf879aacf49c0f614f922dc42.zip
cpython-25de5baf3eaebddbf879aacf49c0f614f922dc42.tar.gz
cpython-25de5baf3eaebddbf879aacf49c0f614f922dc42.tar.bz2
bpo-30980: Fix double close in asyncore.file_wrapper (#2789) (#2898)
* bpo-30980: Fix close test to fail test_close_twice was not considering the fact that file_wrapper is duping the file descriptor. Closing the original descriptor left the duped one open, hiding the fact that close protection is not effective. * bpo-30980: Fix double close protection Invalidated self.fd before closing, handling correctly the case when os.close raises. * bpo-30980: Fix fd leak introduced in the fixed test
Diffstat (limited to 'Lib')
-rw-r--r--Lib/asyncore.py3
-rw-r--r--Lib/test/test_asyncore.py5
2 files changed, 6 insertions, 2 deletions
diff --git a/Lib/asyncore.py b/Lib/asyncore.py
index 705e406..03d1683 100644
--- a/Lib/asyncore.py
+++ b/Lib/asyncore.py
@@ -619,8 +619,9 @@ if os.name == 'posix':
def close(self):
if self.fd < 0:
return
- os.close(self.fd)
+ fd = self.fd
self.fd = -1
+ os.close(fd)
def fileno(self):
return self.fd
diff --git a/Lib/test/test_asyncore.py b/Lib/test/test_asyncore.py
index dc2f716..07edf22 100644
--- a/Lib/test/test_asyncore.py
+++ b/Lib/test/test_asyncore.py
@@ -433,7 +433,10 @@ class FileWrapperTest(unittest.TestCase):
f = asyncore.file_wrapper(fd)
os.close(fd)
- f.close()
+ os.close(f.fd) # file_wrapper dupped fd
+ with self.assertRaises(OSError):
+ f.close()
+
self.assertEqual(f.fd, -1)
# calling close twice should not fail
f.close()