Skip to content

[util] Shut down scheduled executor in Wait.poll to fix thread leak - #4955

Merged
brendandburns merged 1 commit into
kubernetes-client:masterfrom
yue9944882:fix/util-wait-executor-leak
Oct 23, 2026
Merged

brendandburns merged 1 commit into
kubernetes-client:masterfrom
yue9944882:fix/util-wait-executor-leak

Conversation

@yue9944882

Copy link
Copy Markdown
Member

What type of PR is this?

/kind bug

Bug

io.kubernetes.client.util.wait.Wait#poll creates a single-thread ScheduledExecutorService but never shuts it down. The default thread factory produces non-daemon threads, so every poll() call leaves a lingering worker thread alive after the method returns. This leaks a thread on each invocation and can prevent clean JVM shutdown. future.cancel(true) only stops the scheduled task; it does not release the executor's worker thread.

Fix

Shut the executor down in a finally block so it is released on every exit path (loop success, exception, and timeout). All existing return values are preserved — the change is behavior-preserving except for releasing the leaked thread.

} finally {
  future.cancel(true);
  executorService.shutdownNow();
}

Verification

Added a focused regression test WaitTest#pollShutsDownExecutorThreadAfterReturning that captures the executor thread the condition runs on and asserts it has terminated after poll returns.

  • Before fix: Tests run: 1, Failures: 1 (worker thread stays alive)
  • After fix: Tests run: 1, Failures: 0

Ran: ./mvnw -pl util -am -Dtest=WaitTest -Dsurefire.failIfNoSpecifiedTests=false test

@kubernetes-prow kubernetes-prow Bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. 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 Oct 22, 2026
@kubernetes-prow kubernetes-prow Bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 22, 2026
Wait.poll created a single-thread ScheduledExecutorService but never
shut it down. The default thread factory produces non-daemon threads,
so every poll() call left a lingering worker thread alive after
returning, leaking a thread (and preventing clean JVM shutdown) on
each invocation.

Shut the executor down in a finally block so it is released on every
exit path, preserving all existing return values. Adds a regression
test that fails before and passes after the fix.
@yue9944882
yue9944882 force-pushed the fix/util-wait-executor-leak branch from 70f36fe to 97ad1ec Compare October 22, 2026 17:27
@brendandburns
brendandburns marked this pull request as ready for review October 23, 2026 13:56
@kubernetes-prow kubernetes-prow Bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 23, 2026

@brendandburns brendandburns left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

/lgtm
/approve

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

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns, yue9944882

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:
  • OWNERS [brendandburns,yue9944882]

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

@brendandburns
brendandburns merged commit 8f624bf into kubernetes-client:master Oct 23, 2026
20 of 23 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. 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