[Python-checkins] bpo-30980: Fix double close in asyncore.file_wrapper (#2789) (#2898)

Victor Stinner webhook-mailer at python.org
Wed Jul 26 19:27:11 EDT 2017


https://github.com/python/cpython/commit/25de5baf3eaebddbf879aacf49c0f614f922dc42
commit: 25de5baf3eaebddbf879aacf49c0f614f922dc42
branch: 3.6
author: Nir Soffer <nirsof at gmail.com>
committer: Victor Stinner <victor.stinner at gmail.com>
date: 2017-07-27T01:27:08+02:00
summary:

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

files:
M Lib/asyncore.py
M Lib/test/test_asyncore.py

diff --git a/Lib/asyncore.py b/Lib/asyncore.py
index 705e4068130..03d16838b73 100644
--- a/Lib/asyncore.py
+++ b/Lib/asyncore.py
@@ -619,8 +619,9 @@ def getsockopt(self, level, optname, buflen=None):
         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 dc2f716e0bb..07edf2275be 100644
--- a/Lib/test/test_asyncore.py
+++ b/Lib/test/test_asyncore.py
@@ -433,7 +433,10 @@ def test_close_twice(self):
         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()



More information about the Python-checkins mailing list