Skip to content

Track engaged clusters atomically and keep re-engaged clusters - #213

Merged
kcp-ci-bot merged 2 commits into
kcp-dev:mainfrom
Wompipomp:fix/cluster-tracker-race
Dec 2, 2026
Merged

kcp-ci-bot merged 2 commits into
kcp-dev:mainfrom
Wompipomp:fix/cluster-tracker-race

Conversation

@Wompipomp

Copy link
Copy Markdown
Contributor

Summary

clusterTracker.Engage() read its map without holding the lock (a concurrent map read and map
write can crash the agent), and it only recorded a cluster if no entry existed yet. When a cluster
was engaged again with a new context, the tracker kept the old engagement; once that ended, the
entry was deleted although the cluster was engaged again, and sync controllers started later were
not pre-seeded with it.

The entry is now checked and set under the lock and replaced when the context or the cluster
changes. Each engagement removes the entry only if it is still its own (via context.AfterFunc
instead of a goroutine per engagement). Forwarding the engagement to the running controllers is
unchanged and still happens outside the tracker lock. A late Engage() with an already cancelled
context is ignored, so it neither replaces the live entry nor (for a different cluster object)
cancels the live engagement in the running controllers.

Tested with go test -race ./..., make lint and make test-e2e (kcp 0.32). Three new unit tests: concurrent engagements
(fails under -race without the fix), a re-engaged cluster that must stay tracked after its
previous engagement ends (fails without the fix), and a late engagement with a cancelled context
that must not replace the live one (guards the replace logic introduced here; fails with only the
first commit).

Independent of #191, #201, #212 and #211; the branch merges cleanly with each of them.

What Type of PR Is This?

/kind bug

Related Issue(s)

Fixes #209

Release Notes

Fix a possible crash (concurrent map access) in the cluster tracker and sync controllers not being pre-seeded with workspaces that were engaged again.

The cluster tracker read its map without holding the lock, so concurrent
Engage() calls could crash the agent with a concurrent map read and map
write. It also only recorded a cluster if no entry existed yet: when a
cluster was engaged again with a new context, the tracker kept the old
engagement, and once that ended the entry was deleted although the cluster
was still engaged. Sync controllers started afterwards were not pre-seeded
with that cluster.

Check and set the entry under the lock, replace it when the context or the
cluster changes, and let an ended engagement only remove its own entry.

on-behalf-of: @eon-se opensource@eon.com
Signed-off-by: Mark Altmann <mark.altmann.external@eon.com>
A late Engage() whose context is already cancelled must neither replace
the tracked engagement nor be forwarded: a controller would cancel its
live engagement in favour of the ended one when the cluster differs.

on-behalf-of: @eon-se opensource@eon.com
Signed-off-by: Mark Altmann <mark.altmann.external@eon.com>
@kcp-ci-bot kcp-ci-bot added kind/bug Categorizes issue or PR as related to a bug. release-note Denotes a PR that will be considered when it comes time to generate release notes. 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/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 28, 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

Copy link
Copy Markdown
Contributor

@mjudeikis-bot review please

@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: I built internal/kcp and ran go vet on it, both cleanly. I ran the three new tests under go test -race -count=5 on the PR head, and all passed with no race reports. I also copied the new test file into an extracted base tree (915bacb) and ran it there with -race. It reported 3 DATA RACE warnings, and TestConcurrentClusterEngagements and TestReEngagedClusterStaysTracked failed. So the tests reproduce both the unlocked map read and the deletion of a re-engaged cluster, and the change fixes both. I also compared the dedup rule with mcController.Engage in multicluster-runtime v0.24.1, and the lock order between StartController (runnablesLock, then the tracker lock) and Engage (tracker lock released before runnablesLock). I found no deadlock or new ordering problem. I did not run an end-to-end check against a real kcp, because the sandbox has none. The change is correct and well-scoped. The only gap is that no test covers the new rule that ended engagements are not forwarded to controllers.

Comment on lines +183 to +188
// a late Engage() for an engagement that has already ended must neither replace the
// tracked engagement nor be forwarded, as a controller would cancel its live engagement
// in favour of the ended one if the cluster differs
if ctx.Err() != nil {
return nil
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note: The early return for an already-cancelled ctx also stops the call from being forwarded to running controllers. The comment says this forwarding is the main reason for the check: a controller would otherwise cancel its live engagement in favour of the ended one. No test covers it, though. newTestDMCM leaves runnables nil, so TestEndedEngagementDoesNotReplaceLiveOne only checks the tracker map, and that test also passed at the base commit. Add a test that registers a fake mcmanager.Runnable in dmcm.runnables and asserts its Engage is not called for the cancelled ctx.

@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: 72a4421ca5cb642fd144c049eb615eb58f138d0e

@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 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
@Wompipomp

Copy link
Copy Markdown
Contributor Author

/retest

@kcp-ci-bot
kcp-ci-bot merged commit 3b2b245 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/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.

bug: cluster tracker reads its map without the lock and loses re-engaged workspaces

4 participants