Skip to content

Commit 25de5ba

Browse files
nirsvstinner
authored andcommitted
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
1 parent bb7fd3f commit 25de5ba

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Lib/asyncore.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,9 @@ def getsockopt(self, level, optname, buflen=None):
619619
def close(self):
620620
if self.fd < 0:
621621
return
622-
os.close(self.fd)
622+
fd = self.fd
623623
self.fd = -1
624+
os.close(fd)
624625

625626
def fileno(self):
626627
return self.fd

Lib/test/test_asyncore.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,10 @@ def test_close_twice(self):
433433
f = asyncore.file_wrapper(fd)
434434
os.close(fd)
435435

436-
f.close()
436+
os.close(f.fd) # file_wrapper dupped fd
437+
with self.assertRaises(OSError):
438+
f.close()
439+
437440
self.assertEqual(f.fd, -1)
438441
# calling close twice should not fail
439442
f.close()

0 commit comments

Comments
 (0)