Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

stream, ws: disallow timeout<0 when provided timeout is >=0 - #143

Closed
TomasTomecek wants to merge 1 commit into
kubernetes-client:masterfrom
TomasTomecek:master
Closed

TomasTomecek wants to merge 1 commit into
kubernetes-client:masterfrom
TomasTomecek:master

Conversation

@TomasTomecek

@TomasTomecek TomasTomecek commented Jul 4, 2019 •

Copy link
Copy Markdown

Fixes kubernetes-client/python#602
Fixes #106

All the credits go to @PaulFurtado who discovered the solution - I'm just trying to upstream it.

@k8s-ci-robot

Copy link
Copy Markdown
Contributor

Welcome @TomasTomecek!

It looks like this is your first PR to kubernetes-client/python-base 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/python-base has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot

Copy link
Copy Markdown
Contributor

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.


Details

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. I understand the commands that are listed here.

@k8s-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: TomasTomecek
To complete the pull request process, please assign mbohlool
You can assign the PR to them by writing /assign @mbohlool in a comment when ready.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot
k8s-ci-robot requested review from mbohlool and roycaihw July 4, 2019 09:05
@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 4, 2019
@PaulFurtado

Copy link
Copy Markdown

lgtm 👍

@TomasTomecek

Copy link
Copy Markdown
Author

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.

@micw523

micw523 commented Jul 9, 2019

Copy link
Copy Markdown
Contributor

Looks like your change caused EXEC to hang and that's the failure.

@roycaihw

roycaihw commented Jul 9, 2019

Copy link
Copy Markdown
Member

/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

@alejandrox1

Copy link
Copy Markdown

/cc

@k8s-ci-robot

Copy link
Copy Markdown
Contributor

@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.

Details

In response to this:

/cc

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.

@PaulFurtado

Copy link
Copy Markdown

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
We should do some print debugging. It's possible it only hangs at line 112, where that update call should never read anything new.

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
Which explains why the timeout was hacked in using that invalid call to select. It appears to be not entirely trivial to modify websocket-client to add timeouts to individual reads, but it would be possible...

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?

@TomasTomecek

Copy link
Copy Markdown
Author

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

@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 12, 2019
@TomasTomecek

Copy link
Copy Markdown
Author

/check-cla

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jul 12, 2019
@TomasTomecek TomasTomecek changed the title stream: don't use select() since it can block forever stream, ws: raise TimeoutError when timed out Jul 12, 2019
@TomasTomecek

Copy link
Copy Markdown
Author

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-io

codecov-io commented Jul 12, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #143 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #143   +/-   ##
=======================================
  Coverage   93.44%   93.44%           
=======================================
  Files          13       13           
  Lines        1374     1374           
=======================================
  Hits         1284     1284           
  Misses         90       90

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 95858d5...b95a6fd. Read the comment docs.

@TomasTomecek TomasTomecek changed the title stream, ws: raise TimeoutError when timed out stream, ws: disallow timeout<0 when provided timeout is >=0 Jul 16, 2019
@TomasTomecek

Copy link
Copy Markdown
Author

I finally figured this out (thanks to my colleagues and a good lunch).

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.

The other thing is that the websocket library actually does select() on its own, which on the other hand, shouldn't be a problem.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jul 16, 2019
@PaulFurtado

Copy link
Copy Markdown

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.

Comment thread stream/ws_client.py
else:
del self._channels[channel]
return ret
self.update(timeout=(timeout - time.time() + start))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

_request_timeout = kwargs.get("_request_timeout", 60)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread stream/ws_client.py
self._connected = False
return
r, _, _ = select.select(
(self.sock.sock, ), (), (), timeout)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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>
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 17, 2019

@roycaihw roycaihw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread stream/ws_client.py
start = time.time()
while self.is_open() and time.time() - start < timeout:
while self.is_open():
remain = timeout - (time.time() - start)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use remain in L118 as well?

self.update(timeout=remain)

Comment thread stream/ws_client.py
while self.is_open() and time.time() - start < timeout:
while self.is_open():
remain = timeout - (time.time() - start)
if remain < 0:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also when remain is equal to 0?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

select should not be used in WSClient because python-websockets does its own buffering Kubes ops communicating via websockets and API hanging

7 participants