Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Cyborg)
  • No Skin
Collapse
Brand Logo

CIRCLE WITH A DOT

  1. Home
  2. Uncategorized
  3. πŸ«€ SO_KEEPALIVE β€” How your server detects dead connections before the client knowsA client connects to your server.

πŸ«€ SO_KEEPALIVE β€” How your server detects dead connections before the client knowsA client connects to your server.

Scheduled Pinned Locked Moved Uncategorized
linuxnetworkingsystemsprogrammserverdevelopme
1 Posts 1 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • marcelschmall@infosec.exchangeM This user is from outside of this forum
    marcelschmall@infosec.exchangeM This user is from outside of this forum
    marcelschmall@infosec.exchange
    wrote last edited by
    #1

    πŸ«€ SO_KEEPALIVE β€” How your server detects dead connections before the client knows
    A client connects to your server. Then their laptop lid closes. WiFi drops. Router reboots.
    The TCP connection is dead β€” but your server has no idea. It just sits there. Holding a socket. Waiting forever. πŸ‘»

    This is called a half-open connection β€” one of TCP’s most silent failure modes.

    πŸ”§ The fix β€” one line:
    setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &flag, sizeof(flag));

    The kernel now sends small probe packets on idle connections. No response after a few tries? Connection gets cleaned up automatically.

    ⏱️ Three knobs you control:
    β†’ tcp_keepalive_time β€” idle time before first probe (default: 2h 😱)
    β†’ tcp_keepalive_intvl β€” time between probes (default: 75s)
    β†’ tcp_keepalive_probes β€” failures before giving up (default: 9)
    The defaults are hilariously conservative. For a real server you want minutes, not hours.

    πŸ’€ Without it you risk:
    β†’ File descriptor leaks
    β†’ Thread pool exhaustion
    β†’ Memory piling up for connections that died hours ago

    🎯 Who needs it most:
    β†’ WebSockets & long-lived connections
    β†’ Servers behind NAT β€” routers silently drop idle mappings
    β†’ Any server where clients disappear without sending FIN

    🐧 Your server shouldn’t mourn connections that are already gone.

    #Linux #Networking #SystemsProgramming #ServerDevelopment

    1 Reply Last reply
    1
    0
    • R relay@relay.infosec.exchange shared this topic
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups