-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-109370: Support closing Connection and PipeConnection from other thread #109397
gh-109370: Support closing Connection and PipeConnection from other thread #109397
Conversation
serhiy-storchaka
commented
Sep 14, 2023
•
edited by bedevere-app
bot
Loading
edited by bedevere-app
bot
- Issue: Unexpected traceback output in test_concurrent_futures and crash #109370
I do not like this.
I have not tested the code on Windows yet, very likely more try/exept or return code checks should be added in Windows specific code. |
_winapi.PeekNamedPipe(self._handle)[0] != 0): | ||
return True | ||
except OSError as err: | ||
if err.errno == errno.EBADF: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not get EBADF. We should just not call functions on closed handle. If we land in this case, we failed badly before, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can we know that it was closed in other thread without using locks?
You can get EBADF in the following scenario:
- Thread A reads
self._handle
and puts in on the stack. - Thread B closes it.
- Thread A calls function with an already closed file descriptor on the stack.
- That function fails with EBADF errno.
It is the bast case. The worst case is:
- Thread A reads
self._handle
and puts in on the stack. - Thread B closes it.
- Thread B or C opens a new file descriptor which reuses the same integer value.
- Thread A calls function with a file descriptor which now refers to unrelated file or socket.
- Unpredictable consequences.
raise | ||
except TypeError: | ||
self._check_closed() | ||
raise |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to split this change in two parts? Keep this change for _check_closed() change which now raises BrokenPipeError, and you changes around close(): these ones look straightforward are correct. But write a separated PR to add these try/except?
I dislike these try/except. If there is a risk to land into EBADF case with the current code, maybe some kind of locking is needed to not call the Windows API with a handle, while another thread call close() which invalidates the handle.
The more I look at this, the more I dislike it. I do not see how is it possible to safely close a file descriptor on other thread without locking. Making Connection and PipeConnection truly thread-safe in a task on completely different level. I opened #109780 instead. |
Maybe we should just give up and document that they are not thread-safe. I recall that I did that once on some asyncio classes. |