Skip to content

fix(stream): handle tuple _request_timeout format in stream websocket calls - #2786

Merged
kubernetes-prow[bot] merged 5 commits into
kubernetes-client:masterfrom
YuvrajLamba01:stream-tuple-request-timeout-2292
Nov 22, 2026
Merged

kubernetes-prow[bot] merged 5 commits into
kubernetes-client:masterfrom
YuvrajLamba01:stream-tuple-request-timeout-2292

Conversation

@YuvrajLamba01

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

According to the API client documentation and kubernetes/client/rest.py:211-246, _request_timeout can be passed either as a single numeric value (total timeout) or as a pair (tuple) of (connection, read) timeouts.

When using kubernetes.stream (e.g. connect_get_namespaced_pod_exec), passing a tuple previously caused a crash in WSClient.run_forever:

Exception when calling CoreV1Api->connect_get_namespaced_pod_exec: (0)
Reason: '<' not supported between instances of 'float' and 'tuple'

In addition, create_websocket() was not forwarding the connect timeout component to WebSocket.connect(), causing websocket handshakes to fall back to the default 30-second socket timeout even if a lower timeout was specified.

This PR addresses both issues:

  1. In WSClient.run_forever, unpacks timeout = timeout[1] if timeout is a 2-tuple, avoiding the TypeError and correctly bounding execution by the read timeout.
  2. In create_websocket, forwards _request_timeout[0] (or scalar _request_timeout) to connect_opt['timeout'] for WebSocket.connect().
  3. Forwards _request_timeout through WSClient.__init__, websocket_call, and portforward_call.
  4. Adds unit tests covering scalar and tuple timeout handling across create_websocket, run_forever, websocket_call, and _websocket_request.

Which issue(s) this PR fixes:

Fixes #2292

Special notes for your reviewer:

cc: @roycaihw @yliaog

Does this PR introduce a user-facing change?

Support (connection, read) timeout tuple format for _request_timeout in stream calls.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@kubernetes-prow kubernetes-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. labels Nov 15, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from roycaihw and yliaog November 15, 2026 13:01
@kubernetes-prow kubernetes-prow Bot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Nov 15, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @YuvrajLamba01!

It looks like this is your first PR to kubernetes-client/python 🎉. 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 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. 😃

@kubernetes-prow kubernetes-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 15, 2026
Support (connect, read) tuple format for _request_timeout in
WSClient and create_websocket, matching the urllib3 Timeout contract
used across REST API calls. Unpack read timeout for run_forever
and pass connect timeout to WebSocket.connect().

Signed-off-by: Yuvraj Lamba <yuvrajlamba5151@gmail.com>
@YuvrajLamba01
YuvrajLamba01 force-pushed the stream-tuple-request-timeout-2292 branch from 5860d9b to aab793c Compare November 15, 2026 13:02
@kubernetes-prow kubernetes-prow Bot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Nov 15, 2026
@yliaog

yliaog commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Connect timeout leaks onto post-handshake reads/writes (ws_client.py):
In websocket-client, WebSocket.connect(url, timeout=...) sets self.sock_opt.timeout and applies it to the underlying socket permanently (sock.settimeout(...)).
Passing _request_timeout=(1, 60) or (5, None) sets the socket's read/write timeout to the connect timeout (1s / 5s) even after the handshake finishes. Any slow multi-packet frame in WSClient.update() (recv_data_frame), write_stdin(), or PortForward._proxy() will then raise WebSocketTimeoutException after the connect timeout instead of honoring the read timeout.
Suggested fix: After websocket.connect(url, **connect_opt) returns, reset the socket timeout to the read timeout (e.g. websocket.settimeout(read_timeout), where read_timeout is _request_timeout[1] for a 2-tuple or _request_timeout / None as appropriate — note that previously websocket used the default None timeout for post-connect I/O since WSClient.update() already uses poll()/select() for read timeouts).

@yliaog

yliaog commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Strengthen test_run_forever_tuple_timeout: Currently only asserts isinstance(..., float). Using (1, 10) and asserting 5 < timeout <= 10 (or mocking time.time) would actually verify timeout[1] is used rather than timeout[0].

Add a portforward_call test: Since portforward_call() is also updated to forward _request_timeout, a quick unit test covering it would round out coverage.

yliaog and others added 2 commits November 22, 2026 11:35
Reset the underlying socket timeout via websocket.settimeout(read_timeout) after websocket.connect() returns. When _request_timeout is a (connect, read) tuple or (connect, None), this prevents the connect timeout from leaking onto post-handshake socket reads/writes in WSClient.update(), write_stdin(), and PortForward._proxy().

Strengthen test_run_forever_tuple_timeout to verify timeout[1] is extracted and add unit test verifying portforward_call forwards _request_timeout.

Signed-off-by: Yuvraj Lamba <yuvrajlamba5151@gmail.com>
@YuvrajLamba01

YuvrajLamba01 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review and catch regarding the socket timeout leak, @yliaog!

Addressed all points in commit ad78d540b:

  1. Reset Socket Timeout Post-Handshake:

    • In create_websocket, we unpack read_timeout (_request_timeout[1] for 2-tuples, _request_timeout for scalars, or None when omitted).
    • After websocket.connect(url, **connect_opt) completes, we call websocket.settimeout(read_timeout). This resets the underlying socket to the designated read timeout (or None for poll/select-driven event loops), preventing the connect timeout from leaking into WSClient.update(), write_stdin(), or PortForward._proxy().
  2. Strengthened test_run_forever_tuple_timeout:

    • Updated the test fixture to pass timeout=(1, 10) and assert that the computed remaining timeout satisfies 5 < timeout <= 10, verifying that read timeout timeout[1] is consumed rather than connect timeout timeout[0].
  3. Added Unit Tests:

    • Added test_portforward_call_passes_request_timeout verifying portforward_call() forwards _request_timeout to create_websocket.
    • Added test_create_websocket_no_timeout, test_create_websocket_tuple_timeout_none_read, and verified mock_ws.settimeout assertions across all timeout permutations.

@yliaog

yliaog commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

websocket.settimeout(read_timeout) is called unconditionally even when _request_timeout is omitted (None), which would override a caller's global websocket.setdefaulttimeout(...) if they set one. Guarding it with if _request_timeout is not None: (or only when 'timeout' in connect_opt / tuple) would preserve global defaults when _request_timeout isn't passed — minor edge case, up to you whether it's worth another round-trip.

Signed-off-by: Yuvraj Lamba <yuvrajlamba5151@gmail.com>
@YuvrajLamba01

Copy link
Copy Markdown
Contributor Author

Good catch, @yliaog — guarded websocket.settimeout(read_timeout) with if _request_timeout is not None: and updated test_create_websocket_no_timeout to assert settimeout isn't called when _request_timeout is omitted. Ready for another look whenever you have a moment!

kolli-karthik-accenture pushed a commit to kolli-karthik-accenture/bounded that referenced this pull request Nov 22, 2026
kubernetes-client/python#2786 - fix(stream): handle tuple _request_timeout
  - 3 files, score: 77/100, 2 critical issues

kubernetes-client/python#2762 - Regenerate Python clients with shared legacy
  - 30 files, score: 77/100, 13 critical issues

kubernetes-client/python#2698 - Support _request_timeout in dynamic client
  - 2 files, score: 66/100, clean code

BaaRai/ARTEX#494 - fix(server): do not return database errors
  - 2 files, score: 68/100, 1 security issue

torvalds/GuitarPedal#60 - WebMIDI: keep queued BLE writes
  - 2 files, score: 77/100, clean code
@yliaog

yliaog commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

thanks for the PR

/lgtm
/approve

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 22, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: yliaog, YuvrajLamba01

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

The pull request process is described 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

@kubernetes-prow kubernetes-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 22, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit 5fb3ba1 into kubernetes-client:master Nov 22, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Executing command in pod fails when _request_timeout is passed in tuple format

2 participants