From 8ecf0d2a367eef6bb585b9c2939b0afe0a04a016 Mon Sep 17 00:00:00 2001
From: davidby-influx <72418212+davidby-influx@users.noreply.github.com>
Date: Wed, 9 Dec 2020 11:21:29 -0800
Subject: [PATCH] fix: cp.Mux.Serve() closes all net.Listener instances
 silently on error (#20295)

* fix: cp.Mux.Serve() closes all net.Listener instances silently on error. (#20278)

A customer has seen a rash of "connection refused" errors to the meta node.
This fix ensures that when net.Listener instances are closed because of an
error in Accept(), influxdb logs the error which caused the closures, as well
as any errors in closing the Listeners.

Fixes https://github.com/influxdata/influxdb/issues/20256

(cherry picked from commit 5b98166b056d78662057ff723aab0cb87616f9f4)+
---
 CHANGELOG.md |  1 +
 tcp/mux.go   | 10 +++++++---
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 73d05c3713..b0b4959830 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ v1.8.4 [unreleased]
 -	[#19696](https://github.com/influxdata/influxdb/pull/19697): fix(flux): add durations to Flux logging.
 -	[#20276](https://github.com/influxdata/influxdb/pull/20276): fix(error): unsupported value: +Inf" error not handled gracefully.
 -	[#20277](https://github.com/influxdata/influxdb/pull/20277): fix(query): Group By queries with offset that crosses a DST boundary can fail.
+-	[#202955](https://github.com/influxdata/influxdb/pull/20295): fix: cp.Mux.Serve() closes all net.Listener instances silently on error.
 
 v1.8.3 [2020-09-30]
 -------------------
diff --git a/tcp/mux.go b/tcp/mux.go
index 25dae90d21..38f2d9dc0c 100644
--- a/tcp/mux.go
+++ b/tcp/mux.go
@@ -79,6 +79,7 @@ func (mux *Mux) Serve(ln net.Listener) error {
 			continue
 		}
 		if err != nil {
+			mux.Logger.Printf("tcp.Mux: Listener at %s failed failed to accept a connection, closing all listeners - %s", ln.Addr(), err)
 			// Wait for all connections to be demux
 			mux.wg.Wait()
 
@@ -90,7 +91,9 @@ func (mux *Mux) Serve(ln net.Listener) error {
 				wg.Add(1)
 				go func(ln *listener) {
 					defer wg.Done()
-					ln.Close()
+					if err := ln.Close(); err != nil {
+						mux.Logger.Printf("tcp.Mux: Closing the listener at %s failed - %s", ln.Addr().String(), err)
+					}
 				}(ln)
 			}
 			mux.mu.RUnlock()
@@ -100,9 +103,10 @@ func (mux *Mux) Serve(ln net.Listener) error {
 			dl := mux.defaultListener
 			mux.mu.RUnlock()
 			if dl != nil {
-				dl.Close()
+				if closeErr := dl.Close(); closeErr != nil {
+					mux.Logger.Printf("tcp.Mux: Closing the default listener at %s failed - %s", ln.Addr().String(), closeErr)
+				}
 			}
-
 			return err
 		}