Fix ping instability by serializing async writes with a per-session w…#150
Open
nadav78 wants to merge 1 commit intoSoWeBegin:toybattles_mvsurgefrom
Open
Fix ping instability by serializing async writes with a per-session w…#150nadav78 wants to merge 1 commit intoSoWeBegin:toybattles_mvsurgefrom
nadav78 wants to merge 1 commit intoSoWeBegin:toybattles_mvsurgefrom
Conversation
…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>
05375d3 to
a092430
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
…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.