Skip to content

Release cluster engagements and event handlers of stopped sync controllers - #212

Merged
kcp-ci-bot merged 1 commit into
kcp-dev:mainfrom
Wompipomp:fix/sync-controller-stop-leaks
Dec 2, 2026
Merged

kcp-ci-bot merged 1 commit into
kcp-dev:mainfrom
Wompipomp:fix/sync-controller-stop-leaks

Conversation

@Wompipomp

@Wompipomp Wompipomp commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When the sync manager stops a sync controller, it cancels the controller's context, but the
controller stayed reachable in two places, so neither it nor its goroutines were released. Since
#201, every failed start and every unexpected exit stops a controller; with #211 every
PublishedResource update does as well.

  • DynamicMultiClusterManager passed only the cluster's context to Engage(). The engagement in
    multicluster-runtime lasts until that context is done, so a stopped controller stayed engaged
    with every workspace until the workspace disconnected. The DMCM now derives the engagement
    context from both the cluster's and the controller's context, for regular engagements and for
    pre-seeding alike.
  • controller-runtime's source.Kind never removes its handler from the shared informer. The local
    watches of a sync controller (primary object and related resources) now go through a small cache
    wrapper (controllerutil.HandlerRemovingCache) that removes each registered handler (for all
    AddEventHandler* variants) once the context the informer was requested with ends. For Kind
    sources this is a child of their start context, so it ends with the controller (and also when
    the source's cache sync times out). The kcp-side watches already behave like this.

Assumption: the wrapper relies on source.Kind calling GetInformer with its start context, which
is the case in controller-runtime v0.24. If that changes, the new leak test catches it.

Tested with go test -race ./... and make lint (no new findings) after the rebase; e2e runs in CI.
TestStoppedControllersDoNotLeak starts and stops such a controller 20 times and checks that the
goroutine count returns to its baseline; it fails without either part of the fix (+20 goroutines
without the engagement fix, +40 without the handler removal). Unit tests for the engagement
lifetime and for the cache wrapper are included. #201's TestSyncControllerRecovery now also
checks that the local event handler is removed when the controller stops (its test informer gets a
RemoveEventHandler for that).

Rebased onto main after #201 and #194 (the new service-side related-resource watch also uses the
wrapper).

What Type of PR Is This?

/kind bug

Related Issue(s)

Fixes #208

Release Notes

Fix stopped sync controllers staying engaged with all workspaces and leaving event handlers on the local informers.

@kcp-ci-bot kcp-ci-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. dco-signoff: yes Indicates the PR's author has signed the DCO. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Nov 28, 2026
@kcp-ci-bot

Copy link
Copy Markdown
Contributor

Hi @Wompipomp. Thanks for your PR.

I'm waiting for a kcp-dev member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kcp-ci-bot kcp-ci-bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Nov 28, 2026
@kcp-ci-bot kcp-ci-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 2, 2026
@Wompipomp
Wompipomp force-pushed the fix/sync-controller-stop-leaks branch from dfac077 to b014641 Compare December 2, 2026 10:38
@kcp-ci-bot kcp-ci-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 2, 2026
@mjudeikis

Copy link
Copy Markdown
Contributor

/ok-to-test

@kcp-ci-bot kcp-ci-bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Dec 2, 2026
@mjudeikis

This comment was marked as outdated.

mjudeikis-bot

This comment was marked as outdated.

@mjudeikis

Copy link
Copy Markdown
Contributor

@mjudeikis-bot review. Im looking for breaking changes. Be short and consise

@mjudeikis-bot mjudeikis-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified: go build ./... and go vet ./internal/... both pass. I ran go test -race on internal/kcp, internal/controllerutil, internal/controller/syncmanager and internal/controller/sync, and all passed. I then ran the new kcp tests 5 more times with the race detector (-count=5) and TestSyncControllerRecovery 3 more times (-count=3); none were flaky. To check that TestStoppedControllersDoNotLeak tests what it claims, I removed the HandlerRemovingCache wrapper from it. It then failed with "expected goroutines to return to baseline 17 after 20 controller restarts, got 57", which shows it catches the leak. I could not run an end-to-end test against a real kcp cluster in this sandbox.

The change is correct as far as I checked. controller-runtime v0.24.1's Kind source calls GetInformer with the same context it was started with (passed through PollUntilContextCancel), and that context is also cancelled when WaitForSync times out. So tying handler removal to that context is sound. Both local-side source.TypedKind watches in the sync controller are wrapped, and the AfterFunc chaining in dynamicRunnable.engage ends the engagement when either the cluster or the controller stops, without leaving AfterFuncs behind. I have no findings.

@mjudeikis

Copy link
Copy Markdown
Contributor

/lgtm
/approve

@kcp-ci-bot kcp-ci-bot added the lgtm Indicates that a PR is ready to be merged. label Dec 2, 2026
@kcp-ci-bot

Copy link
Copy Markdown
Contributor

LGTM label has been added.

DetailsGit tree hash: 89b39ef5fdcd38574444018f22c87d264541431c

@kcp-ci-bot kcp-ci-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 2, 2026
@mjudeikis

Copy link
Copy Markdown
Contributor

/retest

1 similar comment
@mjudeikis

Copy link
Copy Markdown
Contributor

/retest

…llers

When the sync manager stops a sync controller, it cancels the
controller's context, but two things kept the stopped controller alive:

* The DynamicMultiClusterManager forwarded Engage() with the cluster's
  context only, so multicluster-runtime kept every stopped controller
  engaged (a cluster map entry plus a goroutine holding the whole
  controller) until the workspace disconnected. Engagements now also
  end when the controller's own context is cancelled.

* controller-runtime's Kind source never removes its event handler from
  the shared informer, so every stopped controller left a handler (and
  two listener goroutines) behind on the local manager's informers. The
  local primary and related-resource watches now use a cache wrapper
  that removes each handler once the context the informer was requested
  with ends; for Kind sources that is a child of their start context.
  The kcp-side sources already do this (multicluster-runtime's Kind
  source).

A new test starts and stops a controller wired like a sync controller
20 times and checks that the goroutine count returns to its baseline.
The sync manager's restart test now also checks that the local event
handler of its sync controller is removed once the controller stops (its
test informer gains a RemoveEventHandler for that).

on-behalf-of: @eon-se opensource@eon.com
Signed-off-by: Mark Altmann <mark.altmann.external@eon.com>
@Wompipomp
Wompipomp force-pushed the fix/sync-controller-stop-leaks branch from b014641 to 74b61b3 Compare December 2, 2026 14:23
@kcp-ci-bot kcp-ci-bot removed the lgtm Indicates that a PR is ready to be merged. label Dec 2, 2026
@kcp-ci-bot
kcp-ci-bot requested a review from mjudeikis December 2, 2026 14:24
@Wompipomp

Copy link
Copy Markdown
Contributor Author

Rebased onto main. pull-api-syncagent-test failed in combination with #211: since #211, TestSyncControllerRecovery also starts and replaces controllers (checkChangedPublishedResource), so the assertion added here (exactly one removed event handler) no longer held. Three handlers were registered and all three were removed. The test now checks that every registered handler is removed. No change outside the test. #215 is rebased on top.

@mjudeikis

Copy link
Copy Markdown
Contributor

/lgtm
/approve

@kcp-ci-bot kcp-ci-bot added the lgtm Indicates that a PR is ready to be merged. label Dec 2, 2026
@kcp-ci-bot

Copy link
Copy Markdown
Contributor

LGTM label has been added.

DetailsGit tree hash: 80a2726521a0ba748237a5bbe0ac5da735f3d36c

@kcp-ci-bot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mjudeikis

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

@kcp-ci-bot
kcp-ci-bot merged commit f7f89b5 into kcp-dev:main Dec 2, 2026
12 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. dco-signoff: yes Indicates the PR's author has signed the DCO. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: stopped sync controllers stay engaged with all workspaces and leave event handlers behind

4 participants