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

Fix additional panic #110040

Merged
merged 2 commits into from May 17, 2022
Merged

Fix additional panic #110040

merged 2 commits into from May 17, 2022

Conversation

astoycos
Copy link
Contributor

@astoycos astoycos commented May 13, 2022

What type of PR is this?

/kind bug

What this PR does / why we need it:

Take the incomingBlock Lock
in blockQueue to ensure there
is not any possibility of sending on a
closed incoming channel if shutdown()
closes it unexpectedly.

In #108080 we missed locking the new incomingBlock in the blockQueue function resulting in a race as shown here

#109975 (comment)

I would still like to be able to repro in a unit test but have been unable to do so yet, going ahead and posting this to start the conversation on how to best cover this failure-mode in testing.

cc @aojea @wojtek-t

Which issue(s) this PR fixes:

Fixes #108518

Special notes for your reviewer:

Does this PR introduce a user-facing change?

N/A

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

N/A

@k8s-ci-robot k8s-ci-robot 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. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels May 13, 2022
@k8s-ci-robot k8s-ci-robot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels May 13, 2022
@astoycos
Copy link
Contributor Author

/sig api-machinery

@astoycos
Copy link
Contributor Author

Hrm unit tests seem to deadlock, investigating now.

@astoycos astoycos force-pushed the fix-panic branch 3 times, most recently from 9a780c6 to 1b60d7b Compare May 13, 2022 19:39
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels May 13, 2022
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 13, 2022
go func() {
<-stopCh
t.Log("Stopping Watchers")
m.stopWatching(int64(i))
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

diff --git a/staging/src/k8s.io/apimachinery/pkg/watch/mux_test.go b/staging/src/k8s.io/apimachinery/pkg/watch/mux_test.go
index 17e5779cfcd..35fe9258bfd 100644
--- a/staging/src/k8s.io/apimachinery/pkg/watch/mux_test.go
+++ b/staging/src/k8s.io/apimachinery/pkg/watch/mux_test.go
@@ -263,11 +263,17 @@ func TestBroadcasterShutdownRace(t *testing.T) {
                // eventbroadcaster, see real usage pattern in startRecordingEvents()
                go func() {
                        <-stopCh
-                       t.Log("Stopping Watchers")
+                       t.Logf("Stopping Watchers %d %d", int64(i), i)
                        m.stopWatching(int64(i))
                }()
        }

see

=== RUN   TestBroadcasterShutdownRace
    mux_test.go:266: Stopping Watchers 2 2
    mux_test.go:266: Stopping Watchers 2 2
--- PASS: TestBroadcasterShutdownRace (0.00s)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh right I forgot about the var capture, that always gets me :)

@aojea
Copy link
Member

aojea commented May 13, 2022

it deadlocks if you send an event in the meantime

diff --git a/staging/src/k8s.io/apimachinery/pkg/watch/mux_test.go b/staging/src/k8s.io/apimachinery/pkg/watch/mux_test.go
index 17e5779cfcd..a5549abbdf8 100644
--- a/staging/src/k8s.io/apimachinery/pkg/watch/mux_test.go
+++ b/staging/src/k8s.io/apimachinery/pkg/watch/mux_test.go
@@ -255,6 +255,7 @@ func TestBroadcasterShutdownRace(t *testing.T) {
        // Add a bunch of watchers
        const testWatchers = 2
        for i := 0; i < testWatchers; i++ {
+               i := i
                _, err := m.Watch()
                if err != nil {
                        t.Fatalf("Unable start event watcher: '%v' (will not retry!)",
 err)
@@ -268,6 +269,12 @@ func TestBroadcasterShutdownRace(t *testing.T) {
                }()
        }
 
+       event := Event{Type: Added, Object: &myType{"foo", "hello world"}}
+       err := m.Action(event.Type, event.Object)
+       if err != nil {
+               t.Fatalf("error sending event: %v", err)
+       }
+
        // Manually simulate m.Shutdown() but change it to force a race scenario
        // 1. Close watcher stopchannel, so watchers are closed independently of the
        // eventBroadcaster

@@ -115,6 +115,8 @@ func (obj functionFakeRuntimeObject) DeepCopyObject() runtime.Object {
// won't ever see that event, and will always see any event after they are
// added.
func (m *Broadcaster) blockQueue(f func()) {
Copy link
Member

@aojea aojea May 13, 2022

Choose a reason for hiding this comment

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

I think that this is the problem, we should rethink this to avoid this blockQueue "hack" as mentioned in the comment

https://go.dev/blog/pipelines

Copy link
Contributor Author

@astoycos astoycos May 14, 2022

Choose a reason for hiding this comment

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

Agreed I'm looking at how we can get rid of this, thanks for the article!

I think the thing I need to work through is how can we ensure that watchers don't end up accidentally receiving events that were sent on m.incoming before they were added/ why exactly we care about that....

I made this diagram to try and help myself figure out this whole flow

@wojtek-t
Copy link
Member

it deadlocks if you send an event in the meantime

@aojea - I think it deadlocks because: (a) watchers have channel size of 0 (so synchronous), (b) nothing is reading from watchers.
So I claim it deadlock with or without changes... (and this issue is actually test issue).

I actually think this change is correct (though I agree we should simplify it, but I would prefer to first fix it and then simplify). Unless you disagree it's correct.

There was a race creating a panic with shutting down
an eventbroadcaster and it's associated watchers. This
test exposes it.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
Ensure we take the incomingBlock Lock
in blockQueue to ensure there
is not any possiblity of sending on a
closed incoming channel.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
@astoycos
Copy link
Contributor Author

astoycos commented May 16, 2022

@aojea - I think it deadlocks because: (a) watchers have channel size of 0 (so synchronous), (b) nothing is reading from watchers.
So I claim it deadlock with or without changes... (and this issue is actually test issue).

I verified this claim (bumped the channel size) and updated the unit test

@wojtek-t
Copy link
Member

This LGTM.

But I would like @aojea to also take a look at this to double check (and confirm that he agrees with "fix first and simplify later").

/assign @aojea

@aojea
Copy link
Member

aojea commented May 17, 2022

_checking

This LGTM.

But I would like @aojea to also take a look at this to double check (and confirm that he agrees with "fix first and simplify later").

/assign @aojea

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 17, 2022
@wojtek-t
Copy link
Member

Made one more pass trying to find a potential problem or deadlock - but that looks fine. The functions passed to blockQueue never take lock and the closing of channels looks fine.

/lgtm
/approve

/priority important-soon

@k8s-ci-robot k8s-ci-robot added priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. and removed needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels May 17, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: astoycos, 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 added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 17, 2022
@k8s-ci-robot k8s-ci-robot merged commit ad2c625 into kubernetes:master May 17, 2022
@k8s-ci-robot k8s-ci-robot added this to the v1.25 milestone May 17, 2022
@roycaihw
Copy link
Member

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels May 17, 2022
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. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EventRecorder can accidentally send to a closed channel and Panic
5 participants