-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Fix Channel.close() race condition in pipe.py #2274
base: main
Are you sure you want to change the base?
Changes from 2 commits
44cf229
f4d7fec
e4d9af9
9b5776a
70741ac
6a6f6fb
bf887b2
a092146
3cf92eb
d9fd423
917e1ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,10 +46,9 @@ def __init__(self): | |
self._closed = False | ||
|
||
def close(self): | ||
self._closed = True | ||
os.close(self._rfd) | ||
os.close(self._wfd) | ||
# used for unit tests: | ||
self._closed = True | ||
|
||
def fileno(self): | ||
return self._rfd | ||
|
@@ -64,7 +63,13 @@ def set(self): | |
if self._set or self._closed: | ||
return | ||
self._set = True | ||
os.write(self._wfd, b"*") | ||
try: | ||
os.write(self._wfd, b"*") | ||
except OSError as e: | ||
if e.errno == 9 and self._closed: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible for this We've already trapped for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the flowing (race condition) can happen
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha, this is the crux of the race condition, got it. For the benefit of future readers of the code, please add a comment here describing the race condition as you just described it. Otherwise we risk someone thinking the same thing I did and removing this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done
pettitpeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# The pipe was closed, no need to do anything | ||
return | ||
raise e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we not also need something analogous to protect the (We might not; again, just raising the question.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the
the pipe. So there is no race condition between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same request, please add a comment here explaining why a similar race catch is not (currently) required here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
def set_forever(self): | ||
self._forever = True | ||
|
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.
@pettitpeon, is there any chance of either of these two
.close()
calls failing, makingself._closed == True
a possibly inaccurate representation of the pipe state?(I would think that a failure of either of these two calls would signal an error state for the pipe such that
self._closed == True
is a better state than== False
... but I thought I'd raise the question.)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.
Yes, it could happen. If either
.close()
fails, it would raise anOSError
. In that case some FD might stay open, but the Pipe is not usable anyways. There is no use-case of using it after.close()
. Worst case scenario, we leak resources until the application exits. The most probable scenario is that the close() exception is not handled and your program terminates anyways.A small improvement would be to have single
try
s on each.close()
. In that case if, the first fails and leaks, we might still close the second one correctly.In any case, I see
.close()
errors as non-handable, we close() file descriptors as a "best-effort" to release the resource, but cannot really do anything if it fails.Not really, after
close()
has been called, the object is disposed and no one should it anyways. The "broken state" is meaningless.Last, the
pipe
is most probably a child ofChannel
and when the channel gets destroyed it tries to re-close, which fails if the pipe was previously closed, but it is disregarded by a catch-all try. This is OK, and aligns with the idea that it is a non-handable error.paramiko/paramiko/channel.py
Line 136 in f4d7fec