Skip to content

Detect dead and half-closed clients in LinuxTcpSocket - #3325

Open
magaflaca wants to merge 2 commits into
Pryaxis:general-develfrom
magaflaca:fix/dead-client-detection
Open

magaflaca wants to merge 2 commits into
Pryaxis:general-develfrom
magaflaca:fix/dead-client-detection

Conversation

@magaflaca

Copy link
Copy Markdown

Problem

Clients that disappear without a clean disconnect keep their slot forever. This happens on mobile all the time (lost signal, app killed, network switch). With enough of them the server fills up with ghosts and nobody can join; together with the listener bug fixed in NyxStudios/TerrariaAPI-Server#, the port then closes for good.

Three causes in LinuxTcpSocket:

  1. IsDataAvailable returns NetworkStream.DataAvailable. RemoteClient.TryRead only starts a read when this is true, so the read that would return 0 bytes on FIN (or fail on a socket error) is never issued, and Terraria never notices the disconnect. IsConnected (TcpClient.Connected) also stays true, because nothing failed yet.
  2. Without TCP keepalive, a peer that vanishes without sending FIN/RST is never detected (and the OS default keepalive is 2 hours).
  3. ReadCallback closes the socket on InvalidOperationException, but an IOException (connection reset / timed out) is only logged. The read callback is never invoked, and the slot stays occupied.

It´s this issue #3321

Fix

  • IsDataAvailable uses Socket.Poll(0, SelectMode.SelectRead), which is true for pending data, FIN and errors. Terraria then runs its normal read path: 0 bytes → PendingTermination.
  • Enable TCP keepalive on accepted sockets (30 s idle, 10 s interval, 3 probes).
  • Treat IOException in ReadCallback like InvalidOperationException and close the socket.

Connected players are unaffected: they send data constantly, and keepalive probes are answered by the client OS.

Changelog

  • Fixed disconnected clients (lost connection, half-closed or reset sockets) never freeing their slot, which could fill the server with ghost connections. Accepted sockets now use TCP keepalive.

@greptile-apps

greptile-apps Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change affects client connections handled by Linux server sockets.

The PR appears safe to merge, with a non-blocking improvement to keep rejected keepalive settings from silently blocking joins.

Findings

  1. P2 Keepalive failures silently block joins ▶

Summary

This PR helps LinuxTcpSocket notice disconnected clients:

  • Linux server sockets detect disconnected clients and close their connections.

One non-blocking improvement remains: rejected keepalive settings should not silently prevent players from joining.

Reviews (1) · Last reviewed commit: "Detect dead and half-closed clients in L..." · Reviewed by Greptile

Comment thread TShockAPI/Sockets/LinuxTcpSocket.cs Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant