Skip to content

Fix ping instability by serializing async writes with a per-session w…#150

Open
nadav78 wants to merge 1 commit intoSoWeBegin:toybattles_mvsurgefrom
nadav78:fix/ping-ghost-sessions
Open

Fix ping instability by serializing async writes with a per-session w…#150
nadav78 wants to merge 1 commit intoSoWeBegin:toybattles_mvsurgefrom
nadav78:fix/ping-ghost-sessions

Conversation

@nadav78
Copy link
Copy Markdown
Contributor

@nadav78 nadav78 commented Mar 26, 2026

…rite queue

Multiple concurrent asio::async_write calls on the same socket are undefined behavior per ASIO docs. Under load (many alive players sending position packets), hundreds of async_write calls per second were outstanding on each player's socket simultaneously, causing the kernel to delay or reorder writes and back up pong (order 72) responses behind position broadcasts.

Replace the fire-and-forget async_write in asyncWriteImpl with a queue: enqueue the serialized packet and start a write only when no write is already in progress. The completion handler pops the sent packet and starts the next one. This ensures exactly one outstanding async_write per session at all times.

The ghost session fix (removePlayerFromRoom on disconnect) was a contributing factor, but the deeper root cause was unserialised async writes — multiple concurrent asio::async_write calls on the same socket are UB per ASIO docs. The write queue fix ensures exactly one outstanding write per session at all times, so pong responses are no longer delayed behind a backlog of position broadcasts.

…rite queue

Multiple concurrent asio::async_write calls on the same socket are undefined
behavior per ASIO docs. Under load (many alive players sending position
packets), hundreds of async_write calls per second were outstanding on each
player's socket simultaneously, causing the kernel to delay or reorder writes
and back up pong (order 72) responses behind position broadcasts.

Replace the fire-and-forget async_write in asyncWriteImpl with a queue:
enqueue the serialized packet and start a write only when no write is already
in progress. The completion handler pops the sent packet and starts the next
one. This ensures exactly one outstanding async_write per session at all times.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@nadav78 nadav78 force-pushed the fix/ping-ghost-sessions branch from 05375d3 to a092430 Compare March 26, 2026 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant