Skip to content

Fix SharedInformer.stop() hang during initial list, spurious watch errors, and calls from event handlers - #2820

Merged
kubernetes-prow[bot] merged 3 commits into
kubernetes-client:masterfrom
ilcm96:informer-stop-hang
Nov 30, 2026
Merged

kubernetes-prow[bot] merged 3 commits into
kubernetes-client:masterfrom
ilcm96:informer-stop-hang

Conversation

@ilcm96

@ilcm96 ilcm96 commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

This PR fixes three problems in SharedInformer.stop(), one commit each.

  1. stop() can block forever if called during the initial list, e.g. at shutdown right after start(): the loop then opens a watch without checking the stop event and, with the default resync_period=0, waits for an event that may never come. The loop now checks the stop event before opening the watch.
  2. Every stop() of an active watch logs Unexpected error in watch loop; reconnecting with a ProtocolError traceback and calls the ERROR handlers, because stop() shuts the connection down under the blocked read. Read errors raised after stop was requested now end the loop quietly.
  3. Code after stop() in an event handler never runs: stop() joins the informer thread from itself and raises RuntimeError: cannot join current thread. It now skips the join there; the loop still exits, and start() runs it again once it has exited (e.g. after stop() from another thread).

Which issue(s) this PR fixes:

None filed.

Special notes for your reviewer:

stop() still waits for an in-flight list to return.

How I tested it:

  • New unit tests in kubernetes/test/test_informer.py, each failing before its commit.
  • kind, no injected delay:
    • stop() 0.04s into a 0.1s list of 60 ConfigMaps (53 MiB): on master no return 10s later; now it returns at once, with no watch.
    • Stopping an active watch: 1 ERROR handler call and 1 error log on master (5 of 5 runs), none now.
    • Handler calling stop(): on master the code after it did not run; now it does, and start() resumes the informer.
    • Informer and watch e2e tests: 7 passed, as on master.

Does this PR introduce a user-facing change?

Fixed `SharedInformer.stop()` hanging when called during the initial list, reporting its own connection shutdown as a watch error, and raising `RuntimeError` when called from an event handler.

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


If stop() is called during the initial list, self._watch is still None,
so stop() only sets the stop event. The loop then opens a watch without
checking the event again and blocks until the next event, which may
never come.

Check the stop event right after creating the Watch and before opening
the stream, and read self._watch once in stop(). Add a unit test that
stop() returns and no watch is opened.
stop() shuts the watch connection down, so the blocked read raises an
error such as ProtocolError("Response ended prematurely"). The watch
loop treated it as a watch failure: it logged "Unexpected error in
watch loop; reconnecting" with a traceback and called the ERROR
handlers, on every stop() of an active watch.

Exit the loop without logging or calling the ERROR handlers when the
error is raised after stop was requested. Add a unit test that stopping
an active watch logs nothing and calls no ERROR handler.
Handlers run on the informer thread, so stop() called from a handler
joined the current thread and raised "RuntimeError: cannot join current
thread". The rest of the handler never ran, and the error was logged
as an exception in the handler.

Do not join when stop() is called from the informer thread. The loop
still exits because the stop event is set. Keep the thread reference so
that a start() made while the loop is exiting does not run a second
loop. Add a unit test that the handler continues after stop() and that
start() runs the informer again.
@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 29, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from roycaihw and yliaog November 29, 2026 02:38
@kubernetes-prow kubernetes-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 29, 2026
@yliaog

yliaog commented Nov 30, 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 30, 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 30, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit 2ea2c0a into kubernetes-client:master Nov 30, 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.

2 participants