Browse Source

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.
pull/1418/head
Steven Allen 3 years ago
parent
commit
15dab89d35
  1. 15
      p2p/transport/tcp/tcp.go

15
p2p/transport/tcp/tcp.go

@ -2,7 +2,9 @@ package tcp
import ( import (
"context" "context"
"errors"
"net" "net"
"os"
"runtime" "runtime"
"time" "time"
@ -39,13 +41,22 @@ func tryKeepAlive(conn net.Conn, keepAlive bool) {
return return
} }
if err := keepAliveConn.SetKeepAlive(keepAlive); err != nil { 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 return
} }
if runtime.GOOS != "openbsd" { if runtime.GOOS != "openbsd" {
if err := keepAliveConn.SetKeepAlivePeriod(keepAlivePeriod); err != nil { if err := keepAliveConn.SetKeepAlivePeriod(keepAlivePeriod); err != nil {
log.Errorf("Failed set keepalive period: %s", err) log.Errorw("failed set keepalive period", "error", err)
} }
} }
} }

Loading…
Cancel
Save