Fix #49: synchronize Connection.flushQueue against concurrent region threads#75
Open
mliem2k wants to merge 1 commit into
Open
Fix #49: synchronize Connection.flushQueue against concurrent region threads#75mliem2k wants to merge 1 commit into
mliem2k wants to merge 1 commit into
Conversation
…nt region threads Paper's flushQueue() skips synchronization when isMainThread() is true, assuming only one thread can ever be "main". ShreddedPaperTickThread extends TickThread, so every region-tick-pool thread also satisfies isMainThread(). A player's flushQueue() can therefore be reached concurrently from Connection.tick() (the owning region thread), send() called from an async plugin task, and flushQueueInParallel()'s own sweep, none of which coordinate with each other, racing on the same pendingActions queue and reordering packets. Always synchronize on pendingActions so it is only ever drained by one thread at a time.
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.
Summary
Fixes #49. Traced the root cause:
ShreddedPaperTickThread extends TickThread, so every region-tick-pool thread satisfies Paper'sisMainThread()check (MCUtil.isMainThread()delegates toMinecraftServer.isSameThread(), which is patched toTickThread.isTickThread()).Paper's original
flushQueue()skips synchronization on theisMainThread()path, an assumption that's safe in vanilla (only one thread is ever "main") but broken here: a player'sflushQueue()can be reached concurrently fromConnection.tick()on the region thread that owns the player,send()called from an async plugin task on some other thread,flushQueueInParallel()'s own sweep over all players on a pooled tick thread,none of which coordinate with each other. All of these can independently satisfy
isMainThread()and race on the same connection'spendingActionsqueue, reordering packets. This matches the reported symptom (DecentHolograms entity-spawn packet arriving after its metadata packet).The reporter's own workaround (unconditionally synchronizing
flushQueue()) is the right general shape; this PR keeps that approach but removes the now-inaccurateisMainThread()/isPendingbranching entirely, since neither branch is actually safe to leave unsynchronized under ShreddedPaper's threading model.Test plan
./gradlew :shreddedpaper-server:compileJava)