From 9729991d86555577288a80ade7df4aca6b931e1c Mon Sep 17 00:00:00 2001 From: gobomb Date: Mon, 3 Aug 2020 09:30:39 +0000 Subject: [PATCH] let panics propagate up when processLoop panic Kubernetes-commit: 7e4cfecdd7280e0c8ed84029ad09f60e71803104 --- tools/cache/controller.go | 2 +- tools/cache/controller_test.go | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/tools/cache/controller.go b/tools/cache/controller.go index 27a1c52cdf..1724b3a4ef 100644 --- a/tools/cache/controller.go +++ b/tools/cache/controller.go @@ -118,11 +118,11 @@ func (c *controller) Run(stopCh <-chan struct{}) { c.reflectorMutex.Unlock() var wg wait.Group - defer wg.Wait() wg.StartWithChannel(stopCh, r.Run) wait.Until(c.processLoop, time.Second, stopCh) + wg.Wait() } // Returns true once this controller has completed an initial resource listing diff --git a/tools/cache/controller_test.go b/tools/cache/controller_test.go index 2ce1dddf19..4c071281dd 100644 --- a/tools/cache/controller_test.go +++ b/tools/cache/controller_test.go @@ -402,3 +402,49 @@ func TestUpdate(t *testing.T) { testDoneWG.Wait() close(stop) } + +func TestPanicPropagated(t *testing.T) { + // source simulates an apiserver object endpoint. + source := fcache.NewFakeControllerSource() + + // Make a controller that just panic if the AddFunc is called. + _, controller := NewInformer( + source, + &v1.Pod{}, + time.Millisecond*100, + ResourceEventHandlerFuncs{ + AddFunc: func(obj interface{}) { + // Create a panic. + panic("Just panic.") + }, + }, + ) + + // Run the controller and run it until we close stop. + stop := make(chan struct{}) + defer close(stop) + + propagated := make(chan interface{}) + go func() { + defer func() { + if r := recover(); r != nil { + propagated <- r + } + }() + controller.Run(stop) + }() + // Let's add a object to the source. It will trigger a panic. + source.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "test"}}) + + // Check if the panic propagated up. + select { + case p := <-propagated: + if p == "Just panic." { + t.Logf("Test Passed") + } else { + t.Errorf("unrecognized panic in controller run: %v", p) + } + case <-time.After(wait.ForeverTestTimeout): + t.Errorf("timeout: the panic failed to propagate from the controller run method!") + } +}