From 7e36a88068c039123126788d1450ec8fa4593999 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Sat, 11 Apr 2015 22:08:54 -0700 Subject: [PATCH] pkg/proxy: a more reliable way to detect a closed proxy --- pkg/proxy/proxier.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/pkg/proxy/proxier.go b/pkg/proxy/proxier.go index 022b10eafdc..a0bf9256fd9 100644 --- a/pkg/proxy/proxier.go +++ b/pkg/proxy/proxier.go @@ -94,19 +94,22 @@ func (tcp *tcpProxySocket) ProxyLoop(service ServicePortName, myInfo *serviceInf for { if info, exists := proxier.getServiceInfo(service); !exists || info != myInfo { // The service port was closed or replaced. - break + return } - // Block until a connection is made. inConn, err := tcp.Accept() if err != nil { - if info, exists := proxier.getServiceInfo(service); !exists || info != myInfo { - // Then the service port was just closed so the accept failure is to be expected. - break - } if isTooManyFDsError(err) { panic("Accept failed: " + err.Error()) } + + if isClosedError(err) { + return + } + if info, exists := proxier.getServiceInfo(service); !exists || info != myInfo { + // Then the service port was just closed so the accept failure is to be expected. + return + } glog.Errorf("Accept failed: %v", err) continue } @@ -801,3 +804,10 @@ func (proxier *Proxier) iptablesHostPortalArgs(destIP net.IP, destPort int, prot func isTooManyFDsError(err error) bool { return strings.Contains(err.Error(), "too many open files") } + +func isClosedError(err error) bool { + // A brief discussion about handling closed error here: + // https://code.google.com/p/go/issues/detail?id=4373#c14 + // TODO: maybe create a stoppable TCP listener that returns a StoppedError + return strings.HasSuffix(err.Error(), "use of closed network connection") +}