Repository navigation
Lobby: keep incomplete server lines until the rest arrives - #933
moss-orbit-9f339e wants to merge 1 commit into
Conversation
A socket read can end inside a server command. The ragged JSON store joined only the first and the last read of a command, so a command that came in three or more reads was lost or lost its middle part. Each split also logged "Failed to parse JSON". Keep the incomplete last line in a buffer and add the next read to it, as zk_loopback.lua does. Clear the buffer when a new connection starts, so data from an old connection does not join the Welcome command. Process each line in xpcall, so one handler error does not drop the other lines of the read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
So... what if the second half of the command never arrives? The lobby will never again receive a valid command as it constantly tries to concatenate the first half of the command with things that do not match, resulting in nonsense. |
|
The buffer only holds the text after the last newline, and every newline empties it. So a fragment that never gets its second half can spoil at most the one line it is joined to; the commands after that line parse normally. On a live connection the second half cannot go missing: the lobby socket is TCP, so bytes arrive in order or the connection fails, and the server ends every command with The old Written entirely by Claude Code. |
|
Two notes:
|
|
On detecting the end: the server writes every command with On corruption seen in practice: the old reassembly is a likely source. In this PR's live test, master joined the first and last reads of a If you want an extra guard, I can add a cap that drops the buffer, and logs it, if it grows past for example 1 MB without a newline. Written entirely by Claude Code. |
|
sounds reasonable to me @moss-orbit-9f339e should do full audit of various places where server sends data to lobby to confirm it is all serialized and not manipulated directly as text anywhere |
|
I went through Zero-K-Infrastructure master (6185c76c8). Every place the server writes to a lobby connection goes through
Those are the only callers of
One thing I noticed on the way, separate from framing: |
|
Following up on corruption seen in practice, I checked both ends. Server transport: I ran the real Chobby: master's reassembly has a loss I hadn't described yet. When a read ends inside a command name, for example Other things I found that this PR doesn't touch:
I can send these as separate small PRs if you want them. |
A socket read can end inside a server command. The lobby keeps the start of the command in
raggedJsonStoreand tries it again with the first line of the next read. A command that comes in three or more reads never becomes complete, so it is lost. Each split also logsFailed to parse JSON.receiveBufferand add the next read to it, aszk_loopback.luadoes.Connectclears the buffer, so data from an old connection does not joinWelcome(the cases in Sometimes multiplayer shows up empty. #776). Each line runs inxpcall, so a handler error does not drop the other lines of the read.Failed to parse JSON. In one of them, aBattleDebriefingarrives in parts of about 1.2 KB and is still incomplete after the second read, so it is lost. Most of the others are normal two-read splits, which this change also stops logging.Offline test (Lua 5.1 harness): the harness loads the real
interface_zerok.lua,interface_shared.lua,lobby.luaand LCS, and gives_SocketUpdatea fake socket. The commands are newline-terminated, as the server sends them.BattleDebriefing(3.3 KB) in 3 readsFailed to parse JSONerrors in that streamWelcomeWelcomereceived, 2 errorsWelcomereceived, 0 errorsLive test (engine 2026.07.04, menu mode, Chobby with the Zero-K
chobby_config.json, real server zero-k.info:8200): a test widget connects without a login, so the server sends onlyWelcome. Each run connects twice. The first connection uses normal reads. The second wraps the socket so that each read returns at most 64 bytes. Three runs, 120 s each:Welcomereceived,OnConnectWelcomereceivedFailed to parse JSONlinesOn master, the first and last reads joined into text that still parsed, so the lobby took a damaged
Welcome.Fixes #776
This PR was made with AI help (Claude Code); I checked the change and the test.
🤖 Generated with Claude Code