servlet: fix TomcatTransportTest detects write when not ready#12732
Open
themechbro wants to merge 1 commit intogrpc:masterfrom
Open
servlet: fix TomcatTransportTest detects write when not ready#12732themechbro wants to merge 1 commit intogrpc:masterfrom
themechbro wants to merge 1 commit intogrpc:masterfrom
Conversation
97ef4d7 to
9bf9ad1
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.
Fixes #12723
What was happening
In
AsyncServletOutputStreamWriter#runOrBuffer, the application thread was blindly trusting the cachedreadyAndDrainedboolean state. Under certain asynchronous conditions (e.g., TCP window shrinking), Tomcat's internal buffer would fill up, butreadyAndDrainedwas stilltrue. Bypassing the liveisReady()check caused the container to throw anIllegalStateExceptionwhenactionItem.run()was executed.The Fix
This PR updates the primary execution path in
runOrBufferto explicitly evaluateisReady.getAsBoolean()alongside the cached state.If the cached state is true but the container is actually not ready, the thread gracefully drops into the
elseblock, buffers the action into thewriteChain, and attempts to updatereadyAndDrainedtofalse.Handling Concurrent CAS Failures
By forcing threads into the
elseblock whenisReady()evaluates to false, we introduce a safe race condition: multiple threads might try to executewriteState.compareAndSet(true, false)simultaneously.To prevent the losing threads from crashing on the subsequent
checkState, an explicit check for the initial state (if (curState.readyAndDrained)) was added inside the CAS failure block. If a thread started astruebut failed the CAS, it simply means another concurrent thread already safely toggled the state tofalse. The thread performs a safe no-op and exits, allowing Tomcat'sonWritePossible()to eventually drain the queue.Passed local testing via:
./gradlew :grpc-servlet:tomcat9Test --tests "*TomcatTransportTest*" -PskipAndroid=true -PskipCodegen=true