From c22be72adcf203df41b98d981b029ddc74025cef Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 14 Jul 2021 11:52:16 -0700 Subject: [PATCH] fix: avoid logging "invalid argument" errors when setting keepalive This can happen if, e.g., the connection is already dead. I think? See https://discuss.ipfs.io/t/tcp-keepalive-and-setsockopt-invalid-argument/11725 Really, I don't know _what_ was causing this, but there's nothing we can do about it. --- tcp.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tcp.go b/tcp.go index 4dbc726..b870e9d 100644 --- a/tcp.go +++ b/tcp.go @@ -2,7 +2,9 @@ package tcp import ( "context" + "errors" "net" + "os" "runtime" "time" @@ -39,13 +41,22 @@ func tryKeepAlive(conn net.Conn, keepAlive bool) { return } if err := keepAliveConn.SetKeepAlive(keepAlive); err != nil { - log.Errorf("Failed to enable TCP keepalive: %s", err) + // Sometimes we seem to get "invalid argument" results from this function on Darwin. + // This might be due to a closed connection, but I can't reproduce that on Linux. + // + // But there's nothing we can do about invalid arguments, so we'll drop this to a + // debug. + if errors.Is(err, os.ErrInvalid) { + log.Debugw("failed to enable TCP keepalive", "error", err) + } else { + log.Errorw("failed to enable TCP keepalive", "error", err) + } return } if runtime.GOOS != "openbsd" { if err := keepAliveConn.SetKeepAlivePeriod(keepAlivePeriod); err != nil { - log.Errorf("Failed set keepalive period: %s", err) + log.Errorw("failed set keepalive period", "error", err) } } }