Skip to content

Honor Watch.stop() called while the watch request is in flight - #2799

Merged
kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
ilcm96:watch-stop-during-request
Nov 21, 2026
Merged

kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
ilcm96:watch-stop-during-request

Conversation

@ilcm96

@ilcm96 ilcm96 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

If Watch.stop() is called from another thread while Watch.stream() waits for the watch response, stop() has no socket to shut down (#2486), and stream() checks the stop flag only after reading the first line. The stream then blocks until an event arrives, possibly forever on a quiet resource without timeout_seconds. Reconnect requests have the same window.

This PR checks the stop flag after storing the response and before reading it. Since stop() sets the flag before checking the response, either the stream sees the flag or stop() shuts down the socket.

Which issue(s) this PR fixes:

None reported. Follow-up to #2486, which added the socket shutdown in stop().

Special notes for your reviewer:

  • After stop(), a non-2xx raw response from a hand-written function ends the stream without raising; generated APIs and the dynamic client raise earlier.
  • kubernetes.aio already checks the flag before reading.
  • Tested with test_watch_stop_during_request and test_watch_stop_during_reconnect_request in kubernetes/watch/watch_test.py, which fail on master. The window is too short to hit on a real API server, so kind was used only for the existing watch/informer e2e tests.

Does this PR introduce a user-facing change?

`Watch.stop()` called while the watch request is in flight now ends the stream as soon as the response arrives, instead of waiting for the first event.

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


If stop() is called from another thread before the watch response
arrives, there is no response to shut down and the stop flag is only
checked after the first line. The stream then blocks until an event
arrives, possibly forever on a quiet resource. Reconnect requests have
the same window.

Check the stop flag right after the response is stored and before
reading it. Add unit tests for the initial and reconnect requests.
@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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Nov 21, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from roycaihw and yliaog November 21, 2026 10:09
@kubernetes-prow kubernetes-prow Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 21, 2026
@yliaog

yliaog commented Nov 21, 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 21, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

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

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 21, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit d9d9145 into kubernetes-client:master Nov 21, 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/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants