Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
fix: avoid logging "invalid argument" errors when setting keepalive
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Stebalien committed Jul 14, 2021
1 parent 1b96803 commit c22be72
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions tcp.go
Expand Up @@ -2,7 +2,9 @@ package tcp

import (
"context"
"errors"
"net"
"os"
"runtime"
"time"

Expand Down Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit c22be72

Please sign in to comment.