← Back to Pull requests

Set TCP keepalive idle time so idle connections survive NAT #502

Closed
unknown closed this pull request without merging on Oct 27, 2026 15:52 UTC
View on GitHub
unknown unknown commented on Oct 19, 2026 03:15 UTC
Author

Replaces #501, which Mitch handed off. This is his commit with the review changes from #501 squashed in. He's still the author, and I'm listed as co-author.

Problem

TCP connections with keepalive enabled wait for the OS idle time before sending the first probe, typically 2 hours. Common NAT devices and load balancers drop idle connections much sooner (4 minutes for an Azure load balancer, 350 seconds for an AWS NAT gateway), and they do it silently. The next read on such a connection fails with errors like the Connection timed out reported in #495.

Change

TCP Listener and TCP Sender sockets can now get a shorter idle time wherever they already enable keepalive: when "Keep Connection Open" is on, and always for TCP Sender server mode. The new oie.tcp.keepalive.idle system property sets the idle time in seconds and defaults to 120. It's documented in conf/custom.vmoptions and in the "Keep Connection Open" tooltips on both connectors.

When the TCP plugin starts with the server, it reads the OS idle time (net.ipv4.tcp_keepalive_time on Linux) from a new socket and decides once how keepalive sockets are handled:

  • OS value higher than the property: each keepalive socket gets TCP_KEEPIDLE set to the property value.

  • OS value equal or lower: sockets are left alone and keep the OS value, so an admin who already lowered it keeps their setting.

  • OS value can't be read: sockets get the property value.

  • Property 0 or less, or TCP_KEEPIDLE not supported (such as Windows on a JDK without JDK-8308593): sockets are left alone.
  • The decision isn't revisited until the engine restarts. If the OS value changes while the engine is running, sockets that get the property value keep getting it, even if the OS value drops below it. Sockets that are left alone use the OS value, so they follow the change, including a raise above the property value.

    Logging:

  • An invalid property value logs an ERROR and falls back to 120.

  • If the OS rejects the value when it's set on a socket, the first failure logs an ERROR and later ones log at DEBUG.

  • The decision is logged once at INFO, with the OS value when it can be read and what a later OS change does. Each message names the TCP connectors, so it isn't read as a server-wide setting.
  • All of this is logged at server startup, before any TCP connection, because TcpServicePlugin.start() loads SocketUtil.

    The failures are ERROR rather than WARN because the default log4j2.properties only shows ERROR for this logger, and either one means the configured idle time isn't in effect.

    Keepalive is still disabled explicitly when "Keep Connection Open" is off.

    This is a system property rather than a mirth.properties setting because the TCP connector is a plugin. No plugin has its own key in mirth.properties, and reading one would mean adding TCP-specific code to ConfigurationController. WebServiceDispatcher reads http.maxRedirects the same way.

    Behavior changes for release notes

  • New default. On hosts where the OS keepalive idle time is above 120 seconds (the usual 2-hour default), connections that use keepalive now send their first probe after 120 seconds idle. A lower OS setting is kept. To restore the old behavior, add -Doie.tcp.keepalive.idle=0 to custom.vmoptions.

  • The idle time in use is only logged at INFO, which the default log4j2.properties doesn't show. To see it in mirth.log and the Server Log, add a logger for SocketUtil to conf/log4j2.properties:
  •   logger.tcpSocketUtil.name = com.mirth.connect.connectors.tcp.SocketUtil
      logger.tcpSocketUtil.level = INFO
      

    An invalid oie.tcp.keepalive.idle value is logged at ERROR and shows without this.

  • Dead peers are detected sooner. With Linux's default probe settings (75 seconds apart, 9 probes), a peer that disappears from an idle connection now causes Connection timed out after about 13 minutes instead of about 2h11m. The diagnostic in #495 for telling idle drops (about 2h11m) from undelivered ACKs (about 15 minutes) by timing no longer works once this is in.
  • Relationship to #495

    This addresses the most likely cause of the idle-connection drops in #495, but it doesn't change how TcpReceiver logs read errors, which is what #495 asks for. So the issue should stay open.

    ---
    🤖 Drafted with Claude Code

    Diff: 555 lines (+416 / -139) across 6 files Reviewer: pacmano1 Review duration: 12276m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.48) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 555 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.378
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #502"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.1% (MEDIUM) · Higher = larger blast radius
    48.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.8% · Safe to merge
    20.8%