You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Keep listening when the server is full and skip slots being torn down - #284
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:
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.
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.
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.
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).
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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 withCLI.ServerIsFullinstead). Result: once the server is full a single time, the port is closed until restart, even after everyone leaves.FindNextOpenClientSlotonly checks!IsConnected(). A slot whose previous socket is already closed but which is stillIsActive/PendingTerminationis handed to the new client, andUpdateConnectedClientsthen callsReset()on it, dropping the new connection. This is why quick reconnects often fail.Seems like Pryaxis/TShock#3321
Fix
CLI.ServerIsFullmessage throughNetplay.KickClient(same lock as vanilla), close that socket, and keep listening.Pairs with Pryaxis/TShock#3325 (LinuxTcpSocket dead-client detection), which frees the slots that would otherwise keep the server full.