diff --git a/backend/internal/repository/http_upstream_keepalive_linux.go b/backend/internal/repository/http_upstream_keepalive_linux.go index 607a3120cf..68d7cd757e 100644 --- a/backend/internal/repository/http_upstream_keepalive_linux.go +++ b/backend/internal/repository/http_upstream_keepalive_linux.go @@ -2,20 +2,27 @@ package repository -import "syscall" +import ( + "log/slog" + "syscall" +) // tcpUserTimeout is TCP_USER_TIMEOUT from Linux include/uapi/linux/tcp.h; the // standard library's syscall package does not define it on linux/amd64. const tcpUserTimeout = 0x12 // upstreamDialControl sets TCP_USER_TIMEOUT on each upstream socket -// before it connects. -func upstreamDialControl(_, _ string, raw syscall.RawConn) error { +// before it connects. A failure is logged and the dial goes on without the +// option, like the standard library does for the keep-alive options. +func upstreamDialControl(network, _ string, raw syscall.RawConn) error { var sockErr error if err := raw.Control(func(fd uintptr) { sockErr = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, tcpUserTimeout, int(upstreamTCPUserTimeout.Milliseconds())) }); err != nil { - return err + sockErr = err } - return sockErr + if sockErr != nil { + slog.Warn("upstream_tcp_user_timeout_not_set", "network", network, "error", sockErr) + } + return nil } diff --git a/backend/internal/repository/http_upstream_keepalive_linux_test.go b/backend/internal/repository/http_upstream_keepalive_linux_test.go index ec1cfdf14d..c25e7458fc 100644 --- a/backend/internal/repository/http_upstream_keepalive_linux_test.go +++ b/backend/internal/repository/http_upstream_keepalive_linux_test.go @@ -71,3 +71,15 @@ func TestUpstreamDialerSetsKeepAliveAndUserTimeoutOnTheSocket(t *testing.T) { "TCP_USER_TIMEOUT": 30000, }, got) } + +// A socket option the kernel refuses must not fail the dial: TCP_USER_TIMEOUT +// does not apply to a UDP socket, so setting it there returns an error. +func TestUpstreamDialerIgnoresUserTimeoutFailure(t *testing.T) { + packetConn, err := net.ListenPacket("udp", "127.0.0.1:0") + require.NoError(t, err) + t.Cleanup(func() { _ = packetConn.Close() }) + + conn, err := newUpstreamDialer().DialContext(context.Background(), "udp", packetConn.LocalAddr().String()) + require.NoError(t, err) + require.NoError(t, conn.Close()) +}