From c280b7cab7b6dcd918dcaf366e12e75753079c06 Mon Sep 17 00:00:00 2001 From: yaxinlx Date: Wed, 3 May 2017 23:31:50 +0800 Subject: [PATCH] There is a rule in using go channel: never close a channel in the receiver side. fix https://github.com/kubernetes/kubernetes/issues/45215 delete the channel close line change the event channel element type to struct{} go fmt eventCh channel is not essential to be buffered --- pkg/kubelet/eviction/threshold_notifier_linux.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/kubelet/eviction/threshold_notifier_linux.go b/pkg/kubelet/eviction/threshold_notifier_linux.go index f9449b3c5b3..2c196452f0a 100644 --- a/pkg/kubelet/eviction/threshold_notifier_linux.go +++ b/pkg/kubelet/eviction/threshold_notifier_linux.go @@ -90,20 +90,25 @@ func NewMemCGThresholdNotifier(path, attribute, threshold, description string, h }, nil } -func getThresholdEvents(eventfd int, eventCh chan<- int) { +func getThresholdEvents(eventfd int, eventCh chan<- struct{}, stopCh <-chan struct{}) { for { buf := make([]byte, 8) _, err := syscall.Read(eventfd, buf) if err != nil { return } - eventCh <- 0 + + select { + case eventCh <- struct{}{}: + case <-stopCh: + return + } } } func (n *memcgThresholdNotifier) Start(stopCh <-chan struct{}) { - eventCh := make(chan int, 1) - go getThresholdEvents(n.eventfd, eventCh) + eventCh := make(chan struct{}) + go getThresholdEvents(n.eventfd, eventCh, stopCh) for { select { case <-stopCh: @@ -111,7 +116,6 @@ func (n *memcgThresholdNotifier) Start(stopCh <-chan struct{}) { syscall.Close(n.watchfd) syscall.Close(n.controlfd) syscall.Close(n.eventfd) - close(eventCh) return case <-eventCh: glog.V(2).Infof("eviction: threshold crossed")