Repository navigation
stream, ws: disallow timeout<0 when provided timeout is >=0 - #143
TomasTomecek wants to merge 1 commit into
Conversation
|
Welcome @TomasTomecek! |
|
Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA. It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.
DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: TomasTomecek The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
lgtm 👍 |
|
I logged in to the CLA portal since we, as Red Hat, signed the CLA but sadly it seems the bot is not picking it up I also don't understand the CI failure(s): there are no errors. |
|
Looks like your change caused EXEC to hang and that's the failure. |
|
/cc trying to catch up. The select was added in https://github.com/kubernetes-client/python/pull/125/files#diff-4ac213a679f24e620ff4d3e52b274287R160, which I believe models https://github.com/websocket-client/websocket-client/blob/master/websocket/_app.py. I'd like to understand why the ws-client package had it |
|
/cc |
|
@alejandrox1: GitHub didn't allow me to request PR reviews from the following users: alejandrox1. Note that only kubernetes-client members and repo collaborators can review this PR, and authors cannot review their own PRs. DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
The thing that this PR breaks (further) is timeouts. In that, there is now no case where we will ever timeout waiting on a read. I believe that that test has always relied on timeouts to succeed: https://github.com/kubernetes-client/python/blob/master/kubernetes/e2e_test/test_client.py#L94-L113 That said, the websocket-client library offers no way to timeout a call to recv_data_frame: https://github.com/websocket-client/websocket-client/blob/master/websocket/_core.py#L333 I'm having a hard time finding alternative websocket clients that work with python 2 and don't depend on running a twisted event loop 😢. Anyone know of any? |
|
on the other hand, you can set timeout on the connection: https://github.com/websocket-client/websocket-client/blob/master/websocket/_socket.py#L49 the websocket-client is also calling select(): https://github.com/websocket-client/websocket-client/blob/master/websocket/_socket.py#L94 |
|
/check-cla |
|
I updated the PR to raise a TimeoutError exception when the remaining time is <= 0. This change should improve the situation, for more info see the related issues in my original post. |
Codecov Report
@@ Coverage Diff @@
## master #143 +/- ##
=======================================
Coverage 93.44% 93.44%
=======================================
Files 13 13
Lines 1374 1374
=======================================
Hits 1284 1284
Misses 90 90Continue to review full report at Codecov.
|
|
I finally figured this out (thanks to my colleagues and a good lunch). The problem is, that The other thing is that the websocket library actually does select() on its own, which on the other hand, shouldn't be a problem. |
|
Select is also a problem for others because you must pass it a file descriptor number under 1024, so in a big program it's trivial to exceed that. It only ever watches one file descriptor from select, so that means it's trivial to port from |
| else: | ||
| del self._channels[channel] | ||
| return ret | ||
| self.update(timeout=(timeout - time.time() + start)) |
There was a problem hiding this comment.
The problem is, that timeout=(timeout - time.time() + start) can result into timeout being < 0 which means that select would be forever: the question is, how it's possible that such situation would happen - feels like an apiserver problem.
I don't think it's in server side. We have time.time() - start < timeout before those timeout assignment. Doesn't this mean we have a race condition in between that allows negative timeout, which causes select to wait forever?
If it is a race condition, I prefer we return on timeout instead of raise an error, since it's more backwards compatible
There was a problem hiding this comment.
I'm trying to debug this even further and the problematic bit is that websocket_call(...) gets _request_timeout = None by default which results into infinite loop of:
while self.is_open():
self.update()I have no idea why the socket doesn't get closed.
There was a problem hiding this comment.
But when I call stream like this:
stream(
self.api.connect_get_namespaced_pod_exec,
self.pod_name,
self.k8s_namespace_name,
command=command,
stdin=False,
stderr=True,
stdout=True,
tty=False,
_request_timeout=5.0
)it suddenly works just fine and the code is no longer stuck in an infinite loop
There was a problem hiding this comment.
I'm trying to debug this even further and the problematic bit is that websocket_call(...) gets _request_timeout = None by default which results into infinite loop of:
Good catch! I tried locally and also found _request_timeout = None. This is problematic because I would expect it to be defaulted to 60
python-base/stream/ws_client.py
Line 237 in 95858d5
There was a problem hiding this comment.
I am going to close this PR since this seems to be the solution to the problem.
Let me know if it's worth to document it (and I can send a PR).
| self._connected = False | ||
| return | ||
| r, _, _ = select.select( | ||
| (self.sock.sock, ), (), (), timeout) |
There was a problem hiding this comment.
Select is also a problem for others because you must pass it a file descriptor number under 1024, so in a big program it's trivial to exceed that. It only ever watches one file descriptor from select, so that means it's trivial to port from select to poll.
removing the select and asking existing users to wrap their own select or polling (e.g. kubernetes-client/python#881) seems to be a big breaking change to me.
cc @yliaog ^^^
Is the select hanging all the time or is this caused by a race condition in our timeout calculation?
There was a problem hiding this comment.
I agree with you that the change in behaviour is not nice. Since the websocket lib does select() as well, I was hoping to get at least some consistency.
I can reproduce the hang consistently, it usually happens when megabytes of data flow through the exec interface (some buffer fills in?)
I'm gonna revert this change and try the one you propose above.
There was a problem hiding this comment.
@PaulFurtado The statement about "file descriptor number under 1024" sounds reasonable. I didn't know that. I'm not against adding a separate update_with_no_timeout method if that solves the problem with select, as long as we don't break existing users who depend on the timeout interface
Signed-off-by: Tomas Tomecek <ttomecek@redhat.com>
roycaihw
left a comment
There was a problem hiding this comment.
The change looks reasonable. I wonder if it would be possible for you to add a test case that demonstrate this fix? Probably it's non-trivial because it looks like a race condition to me
| start = time.time() | ||
| while self.is_open() and time.time() - start < timeout: | ||
| while self.is_open(): | ||
| remain = timeout - (time.time() - start) |
There was a problem hiding this comment.
use remain in L118 as well?
self.update(timeout=remain)
| while self.is_open() and time.time() - start < timeout: | ||
| while self.is_open(): | ||
| remain = timeout - (time.time() - start) | ||
| if remain < 0: |
There was a problem hiding this comment.
also when remain is equal to 0?
Fixes kubernetes-client/python#602
Fixes #106
All the credits go to @PaulFurtado who discovered the solution - I'm just trying to upstream it.