ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Critical - P2
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Dotnet Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

       
      When a server node freezes (the TCP connection is established, but the server stops responding), a new connection opened by the driver can hang in the TLS handshake indefinitely. ConnectTimeout does not cover the handshake, so the application does not recover after the node is back; it recovers only after a process restart or when the OS TCP keepalive drops the socket (~2 hours with Linux defaults).
       
      Driver: MongoDB.Driver 3.13.0 (also 3.5+ and 2.x).
      Server: MongoDB 8.0, replica set, TLS enabled.
      Client: .NET 8 on Linux.
       
      Code (v3.13.0, src/MongoDB.Driver/Core/Connections/SslStreamFactory.cs):

      • CreateStream, NET6_0_OR_GREATER (line 60): sslStream.AuthenticateAsClient(options) — no timeout and no cancellation token.
      • CreateStream, NETSTANDARD2_1 (line 63) and CreateStreamAsync (line 88): AuthenticateAsClientAsync(options, cancellationToken) — the token has no deadline tied to ConnectTimeout.
      • ConnectTimeout is applied only to the TCP connect (TcpStreamFactory: connectTask.WaitTask(_settings.ConnectTimeout, ...)).
      • SocketTimeout applies to message reads/writes after the connection is established, not to the handshake.
      • TcpStreamFactory sets keepalive to 120 s / 10 s only on Windows (IOControl KeepAliveValues); on Linux it only sets SocketOptionName.KeepAlive = true, so the OS default (tcp_keepalive_time = 7200 s) applies.

        How to Reproduce

        1. Replica set with TLS enabled; client on Linux.
        2. Freeze one node after it accepts TCP (e.g. SIGSTOP the mongod process, or drop its packets with iptables after the TCP connect).
        3. Make the driver open a new connection to that node (pool growth, or after existing connections are closed).
        4. The connection-open thread stays in AuthenticateAsClient; ConnectTimeout does not fire.
        5. Resume the node: the stuck connection is not retried until the OS keepalive closes the socket.
         
        Expected: the TLS handshake is bounded by ConnectTimeout (or a dedicated handshake timeout), so a stalled handshake fails and the connection is retried.

        Additional Background

        The Go driver already bounds the TLS handshake with its connect timeout (GODRIVER-1879).
         
        Suggested fix in the driver:

      • Run the handshake under a token that combines the caller's token with CancelAfter(ConnectTimeout), and use AuthenticateAsClientAsync(options, token) on every target framework, including the NET6+ sync path.
      • Or set ReadTimeout/WriteTimeout on the underlying stream to ConnectTimeout for the duration of the handshake.
      • Apply the 120 s / 10 s keepalive on Linux too (Socket.SetSocketOption TcpKeepAliveTime / TcpKeepAliveInterval on .NET Core 3.0+), not only on Windows.
         
        Workarounds that help applications today:
      • Explicit SocketTimeout (e.g. 3 min for workers) and MaxConnectionIdleTime (e.g. 5 min), so dead pooled connections are dropped and retried.
      • A CancellationToken with a deadline (or MaxTime) on every operation instead of CancellationToken.None, so a request does not wait forever behind a stuck connection.
      • A lower OS keepalive on the host or container (net.ipv4.tcp_keepalive_time = 60–120 s), so a stalled socket is closed in minutes instead of hours.
      • Log the effective client settings at startup, so missing timeouts are visible.

            Assignee:
            Oleksandr Poliakov
            Reporter:
            Yan Papkou (EXT)
            None
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: