bpo-30980: Fix double close in asyncore.file_wrapper#2789
Conversation
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.
|
@nirs, thanks for your PR! By analyzing the history of the files in this pull request, we identified @giampaolo, @ezio-melotti and @pitrou to be potential reviewers. |
|
@Haypo, please review. |
Invalidated self.fd before closing, handling correctly the case when os.close raises.
|
I don't understand this change, can you please elaborate? It make the test failing. Is it supposed to fix a bug? |
|
@Haypo the first patch added a failing test to demonstrate the issue, the second patch fixes it. |
|
The test now leaks a file descriptor: |
| @@ -431,9 +431,11 @@ def test_resource_warning(self): | |||
| def test_close_twice(self): | |||
| fd = os.open(support.TESTFN, os.O_RDONLY) | |||
There was a problem hiding this comment.
Add something like: self.addCleanup(os.close, fd). It's enough to fix "./python -m test -R 3:3 test_asyncore".
| f.close() | ||
| os.close(f.fd) # file_wrapper dupped fd | ||
| with self.assertRaises(OSError): | ||
| f.close() |
There was a problem hiding this comment.
Please add this test here:
self.assertEqual(f.fileno(), -1)
To test your fix.
| with self.assertRaises(OSError): | ||
| f.close() | ||
|
|
||
| self.assertEqual(f.fd, -1) |
There was a problem hiding this comment.
@Haypo isn't this ^^^^ good enough?
Since file_wrapper.fd is not documented, maybe we should replace f.fd with f.fileno()? Do you want to add this change to the patch?
There was a problem hiding this comment.
Oh, I missed it :-) It's enough.
|
Do you want to backport your fix to 3.5 and 3.6? |
|
(ah, and 2.7 too according your bpo.) |
|
@Haypo , I'll send backports later this week, thanks! |
* 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
* 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
* 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
* 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
* 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
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.