Skip to content

Keep listening when the server is full and skip slots being torn down - #284

Open
magaflaca wants to merge 1 commit into
Pryaxis:general-develfrom
magaflaca:fix/connection-accept-full-server
Open

magaflaca wants to merge 1 commit into
Pryaxis:general-develfrom
magaflaca:fix/connection-accept-full-server

Conversation

@magaflaca

Copy link
Copy Markdown

Problem

On TShock 6.x / Terraria 1.4.5, servers randomly stop accepting players: clients hang on "Connecting to IP:port" while the console keeps working normally. Only a restart fixes it.

Two issues in NetHooks.OnConnectionAccepted:

  1. The listener is stopped and never restarted. When the accepted client takes the last free slot, Netplay.TcpListener.StopListening() is called. Terraria 1.4.4 restarted the listener from the server loop once a slot freed up, but 1.4.5 removed that code path (vanilla 1.4.5 never stops listening; it kicks with CLI.ServerIsFull instead). Result: once the server is full a single time, the port is closed until restart, even after everyone leaves.
  2. Slots that are still being torn down are reused. FindNextOpenClientSlot only checks !IsConnected(). A slot whose previous socket is already closed but which is still IsActive / PendingTermination is handed to the new client, and UpdateConnectedClients then calls Reset() on it, dropping the new connection. This is why quick reconnects often fail.

Seems like Pryaxis/TShock#3321

Fix

  • When no slot is free, reject with the vanilla CLI.ServerIsFull message through Netplay.KickClient (same lock as vanilla), close that socket, and keep listening.
  • Only consider a slot free when it is not connected, not active and not pending termination.

Pairs with Pryaxis/TShock#3325 (LinuxTcpSocket dead-client detection), which frees the slots that would otherwise keep the server full.

@defonion

Copy link
Copy Markdown

Tested this together with Pryaxis/TShock#3325 on TShock 6.2.1 (base b25b3e5, Terraria 1.4.5.8, OTAPI 3.3.14), linux-x64, built the same way as CI (dotnet publish -r linux-x64 -f net9.0 -c Release -p:PublishSingleFile=true --no-self-contained, SDK 9.0.321). Test server on 127.0.0.1, -maxplayers 8, no plugins, fresh config. The only differences between builds are LinuxTcpSocket.cs (TShock PRs) and NetHooks.cs (this PR).

Scenario 6.2.1 #3325 only #3327 only #3325 + #284
2 × 40 connect-and-close (scanner-like) port closed after the 9th ✅ ✅ ✅
Hold 8 connections at once, close them, wait 15 s, real handshake ❌ listener never returns (waited 120 s) ❌ listener never returns, 0 CLOSE_WAIT ❌ listener never returns, 0 CLOSE_WAIT ✅ slot assigned (2 rounds)
9th connection while full connection refused connection refused connection refused ✅ CLI.ServerIsFull kick, 20/20
-forceupdate (no PR) – – – scanner leak gone, listener still never returns after full

I also checked the root cause by decompiling Terraria.Netplay from the OTAPI builds: in 1.4.5.6 (TShock 6.1.0) ServerLoop still calls StartListeningIfNeeded(), while in 1.4.5.8 it doesn't, and vanilla OnConnectionAccepted kicks with CLI.ServerIsFull instead of stopping. That matches the description here exactly.

One note in case it helps: I first tried the same change without args.client.Close(). Then the kicked client never gets EOF (20/20 hung until timeout), so the explicit Close() is needed. With it the kick message arrived 20/20 times over loopback. I did not test over a real WAN link.

Minor observation: with #284, in a second "fill all 8 slots" round right after a previous one, the 9th connection sometimes got a slot. I believe that's the intended "skip slots being torn down" behaviour (one of the 8 held sockets was rejected while a slot was still pending termination), not a bug.

So from my side: #3325 + #284 together fix #3321. Either one alone does not.

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.

2 participants