From 5c6292c074e21f91507b04a2ee7284f7f03eac3b Mon Sep 17 00:00:00 2001 From: "Matt T. Proud" Date: Tue, 26 Jul 2016 05:53:21 +0200 Subject: [PATCH] pkg/various: plug leaky time.New{Timer,Ticker}s According to the documentation for Go package time, `time.Ticker` and `time.Timer` are uncollectable by garbage collector finalizers. They leak until otherwise stopped. This commit ensures that all remaining instances are stopped upon departure from their relative scopes. --- pkg/controller/volume/persistentvolume/framework_test.go | 1 + pkg/kubelet/kubelet.go | 2 ++ pkg/kubelet/server/remotecommand/httpstream.go | 1 + pkg/util/goroutinemap/goroutinemap_test.go | 1 + pkg/util/httpstream/spdy/connection_test.go | 1 + .../nestedpendingoperations/nestedpendingoperations_test.go | 1 + 6 files changed, 7 insertions(+) diff --git a/pkg/controller/volume/persistentvolume/framework_test.go b/pkg/controller/volume/persistentvolume/framework_test.go index cd78384daa1..a4ce1e68b85 100644 --- a/pkg/controller/volume/persistentvolume/framework_test.go +++ b/pkg/controller/volume/persistentvolume/framework_test.go @@ -363,6 +363,7 @@ func checkEvents(t *testing.T, expectedEvents []string, ctrl *PersistentVolumeCo // Read recorded events - wait up to 1 minute to get all the expected ones // (just in case some goroutines are slower with writing) timer := time.NewTimer(time.Minute) + defer timer.Stop() fakeRecorder := ctrl.eventRecorder.(*record.FakeRecorder) gotEvents := []string{} diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 5f7e6449173..634b2719b6b 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -2072,7 +2072,9 @@ func (kl *Kubelet) syncLoop(updates <-chan kubetypes.PodUpdate, handler SyncHand // that need to be sync'd. A one-second period is sufficient because the // sync interval is defaulted to 10s. syncTicker := time.NewTicker(time.Second) + defer syncTicker.Stop() housekeepingTicker := time.NewTicker(housekeepingPeriod) + defer housekeepingTicker.Stop() plegCh := kl.pleg.Watch() for { if rs := kl.runtimeState.errors(); len(rs) != 0 { diff --git a/pkg/kubelet/server/remotecommand/httpstream.go b/pkg/kubelet/server/remotecommand/httpstream.go index ccd0c3f0076..83db923960c 100644 --- a/pkg/kubelet/server/remotecommand/httpstream.go +++ b/pkg/kubelet/server/remotecommand/httpstream.go @@ -184,6 +184,7 @@ func createHttpStreamStreams(req *http.Request, w http.ResponseWriter, opts *opt } expired := time.NewTimer(streamCreationTimeout) + defer expired.Stop() ctx, err := handler.waitForStreams(streamCh, opts.expectedStreams, expired.C) if err != nil { diff --git a/pkg/util/goroutinemap/goroutinemap_test.go b/pkg/util/goroutinemap/goroutinemap_test.go index 9e34a088195..73a7411d763 100644 --- a/pkg/util/goroutinemap/goroutinemap_test.go +++ b/pkg/util/goroutinemap/goroutinemap_test.go @@ -484,6 +484,7 @@ func retryWithExponentialBackOff(initialDuration time.Duration, fn wait.Conditio func waitChannelWithTimeout(ch <-chan interface{}, timeout time.Duration) error { timer := time.NewTimer(timeout) + defer timer.Stop() select { case <-ch: diff --git a/pkg/util/httpstream/spdy/connection_test.go b/pkg/util/httpstream/spdy/connection_test.go index d402785e2e8..a73fd1329fc 100644 --- a/pkg/util/httpstream/spdy/connection_test.go +++ b/pkg/util/httpstream/spdy/connection_test.go @@ -146,6 +146,7 @@ func TestConnectionCloseIsImmediateThroughAProxy(t *testing.T) { } expired := time.NewTimer(15 * time.Second) + defer expired.Stop() i := 0 for { select { diff --git a/pkg/volume/util/nestedpendingoperations/nestedpendingoperations_test.go b/pkg/volume/util/nestedpendingoperations/nestedpendingoperations_test.go index f37ec39ce21..3181e616960 100644 --- a/pkg/volume/util/nestedpendingoperations/nestedpendingoperations_test.go +++ b/pkg/volume/util/nestedpendingoperations/nestedpendingoperations_test.go @@ -558,6 +558,7 @@ func retryWithExponentialBackOff(initialDuration time.Duration, fn wait.Conditio func waitChannelWithTimeout(ch <-chan interface{}, timeout time.Duration) error { timer := time.NewTimer(timeout) + defer timer.Stop() select { case <-ch: