Repository navigation
Fix SharedInformer.stop() hang during initial list, spurious watch errors, and calls from event handlers - #2820
Merged
kubernetes-prow[bot] merged 3 commits intoNov 30, 2026
Conversation
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.
Contributor
|
thanks for the PR /lgtm |
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 DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
kubernetes-prow
Bot
merged commit Nov 30, 2026
2ea2c0a
into
kubernetes-client:master
10 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.stop()can block forever if called during the initial list, e.g. at shutdown right afterstart(): the loop then opens a watch without checking the stop event and, with the defaultresync_period=0, waits for an event that may never come. The loop now checks the stop event before opening the watch.stop()of an active watch logsUnexpected error in watch loop; reconnectingwith aProtocolErrortraceback and calls the ERROR handlers, becausestop()shuts the connection down under the blocked read. Read errors raised after stop was requested now end the loop quietly.stop()in an event handler never runs:stop()joins the informer thread from itself and raisesRuntimeError: cannot join current thread. It now skips the join there; the loop still exits, andstart()runs it again once it has exited (e.g. afterstop()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:
kubernetes/test/test_informer.py, each failing before its commit.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.stop(): on master the code after it did not run; now it does, andstart()resumes the informer.Does this PR introduce a user-facing change?
Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.: