Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid race in subsequent changes to the same controller #2220

Merged
merged 1 commit into from
Dec 19, 2022

Conversation

mborsz
Copy link
Member

@mborsz mborsz commented Dec 19, 2022

/kind bug

What this PR does / why we need it:

One of the test runs has failed with

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x17b1e9a]
goroutine 79903 [running]:
k8s.io/perf-tests/clusterloader2/pkg/measurement/common.(*objectChecker).Stop(0x1aa49a0)
	/home/prow/go/src/k8s.io/perf-tests/clusterloader2/pkg/measurement/common/wait_for_controlled_pods.go:713 +0x7a
k8s.io/perf-tests/clusterloader2/pkg/measurement/util/checker.Map.Add(...)
	/home/prow/go/src/k8s.io/perf-tests/clusterloader2/pkg/measurement/util/checker/checker_map.go:43
k8s.io/perf-tests/clusterloader2/pkg/measurement/common.(*waitForControlledPodsRunningMeasurement).handleObjectLocked(0xc03c6c9ba0, {0x0, 0x0}, {0x20edda0, 0xc081055010})
	/home/prow/go/src/k8s.io/perf-tests/clusterloader2/pkg/measurement/common/wait_for_controlled_pods.go:493 +0x1f6
k8s.io/perf-tests/clusterloader2/pkg/measurement/common.(*waitForControlledPodsRunningMeasurement).handleObject(0xc03c6c9ba0, {0x0, 0x0}, {0x1c5b1e0, 0xc081055010})
	/home/prow/go/src/k8s.io/perf-tests/clusterloader2/pkg/measurement/common/wait_for_controlled_pods.go:453 +0x5b5
k8s.io/perf-tests/clusterloader2/pkg/measurement/common.(*waitForControlledPodsRunningMeasurement).start.func1.1()
	/home/prow/go/src/k8s.io/perf-tests/clusterloader2/pkg/measurement/common/wait_for_controlled_pods.go:279 +0x2d
k8s.io/perf-tests/clusterloader2/pkg/measurement/util/workerqueue.(*WorkerQueue).worker(0xc02447dba0)
	/home/prow/go/src/k8s.io/perf-tests/clusterloader2/pkg/measurement/util/workerqueue/workerqueue.go:65 +0x31
k8s.io/apimachinery/pkg/util/wait.(*Group).Start.func1()
	/home/prow/go/pkg/mod/k8s.io/apimachinery@v0.22.15/pkg/util/wait/wait.go:73 +0x5a
created by k8s.io/apimachinery/pkg/util/wait.(*Group).Start
	/home/prow/go/pkg/mod/k8s.io/apimachinery@v0.22.15/pkg/util/wait/wait.go:71 +0x88

This has failed on o.cancel = nil in

func (o *objectChecker) Stop() {
o.lock.Lock()
defer o.lock.Unlock()
o.cancel()

The cancel is being set in

o := newObjectChecker(key)
o.lock.Lock()
defer o.lock.Unlock()
w.handlingGroup.Start(func() {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
o.SetCancel(cancel)

The problem is that the setting it happens asynchronously (in the separate goroutine) and when the function finishes there is potential race between calling o.cancel and o.SetCancel.

If the object is being changed between o.SetCancel is called, then the attempt to replace it (and Stop previous instance) will fail as here.

The idea for fix is to:

  • make invariant that when the waitForRuntimeObject finishes (so before it's pushed to checkerMap) it has o.cancel set.
  • context.WithCancel requires calling cancel as soon as the code finishes to clean resources, so let's keep it where it was (in the async goroutine).

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

/assign @tosi3k

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Dec 19, 2022
@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 19, 2022
@mborsz
Copy link
Member Author

mborsz commented Dec 19, 2022

/assign @marseel

@wojtek-t
Copy link
Member

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 19, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mborsz, wojtek-t

The full list of commands accepted by this bot can be found here.

The pull request process is described here

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

@k8s-ci-robot k8s-ci-robot merged commit ce673a9 into kubernetes:master Dec 19, 2022
ashishranjan738 pushed a commit to ashishranjan738/perf-tests that referenced this pull request Dec 21, 2022
Avoid race in subsequent changes to the same controller
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/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants