-
Notifications
You must be signed in to change notification settings - Fork 84
Fix Envoy Mobile bug where writing prevents the read loop from running, #2221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
goaway
merged 9 commits into
envoyproxy:main
from
RyanTheOptimist:AsyncOnSendWindowAvailable
May 10, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a943abc
Fix Envoy Mobile bug where writing prevents the read loop from running,
RyanTheOptimist 63ac2b7
format
RyanTheOptimist bc5ba5a
Add missing test file
RyanTheOptimist 9070956
Add unit test coverage
RyanTheOptimist b8c62e0
Reuse scheduled_callback_
RyanTheOptimist 08b8adf
comment
RyanTheOptimist 648522a
Always schedule a new callback to capture new state
RyanTheOptimist 123ce21
Merge branch 'main' into AsyncOnSendWindowAvailable
RyanTheOptimist c2107cc
Add comment
RyanTheOptimist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
114 changes: 114 additions & 0 deletions
114
test/java/org/chromium/net/testing/AndroidEnvoyExplicitH2FlowTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package org.chromium.net.testing; | ||
|
|
||
| import static io.envoyproxy.envoymobile.engine.EnvoyConfiguration.TrustChainVerification.ACCEPT_UNTRUSTED; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.chromium.net.testing.CronetTestRule.SERVER_CERT_PEM; | ||
| import static org.chromium.net.testing.CronetTestRule.SERVER_KEY_PKCS8_PEM; | ||
|
|
||
| import android.content.Context; | ||
| import androidx.test.core.app.ApplicationProvider; | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
| import io.envoyproxy.envoymobile.AndroidEngineBuilder; | ||
| import io.envoyproxy.envoymobile.Engine; | ||
| import io.envoyproxy.envoymobile.LogLevel; | ||
| import io.envoyproxy.envoymobile.RequestHeaders; | ||
| import io.envoyproxy.envoymobile.RequestHeadersBuilder; | ||
| import io.envoyproxy.envoymobile.RequestMethod; | ||
| import io.envoyproxy.envoymobile.Stream; | ||
| import io.envoyproxy.envoymobile.UpstreamHttpProtocol; | ||
| import io.envoyproxy.envoymobile.engine.AndroidJniLibrary; | ||
| import java.net.URL; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| @RunWith(AndroidJUnit4.class) | ||
| public class AndroidEnvoyExplicitH2FlowTest { | ||
|
|
||
| private Engine engine; | ||
|
|
||
| @BeforeClass | ||
| public static void loadJniLibrary() { | ||
| AndroidJniLibrary.loadTestLibrary(); | ||
| } | ||
|
|
||
| @Before | ||
| public void setUpEngine() throws Exception { | ||
| CountDownLatch latch = new CountDownLatch(1); | ||
| Context appContext = ApplicationProvider.getApplicationContext(); | ||
| engine = new AndroidEngineBuilder(appContext) | ||
| .setTrustChainVerification(ACCEPT_UNTRUSTED) | ||
| .addLogLevel(LogLevel.DEBUG) | ||
| .setOnEngineRunning(() -> { | ||
| latch.countDown(); | ||
| return null; | ||
| }) | ||
| .build(); | ||
| Http2TestServer.startHttp2TestServer(appContext, SERVER_CERT_PEM, SERVER_KEY_PKCS8_PEM); | ||
| latch.await(); // Don't launch a request before initialization has completed. | ||
| } | ||
|
|
||
| @After | ||
| public void shutdown() throws Exception { | ||
| engine.terminate(); | ||
| Http2TestServer.shutdownHttp2TestServer(); | ||
| } | ||
|
|
||
| @Test | ||
| public void continuousWrite_withCancelOnResponseHeaders() throws Exception { | ||
| URL url = new URL(Http2TestServer.getEchoAllHeadersUrl()); | ||
| RequestHeadersBuilder requestHeadersBuilder = new RequestHeadersBuilder( | ||
| RequestMethod.POST, url.getProtocol(), url.getAuthority(), url.getPath()); | ||
| RequestHeaders requestHeaders = | ||
| requestHeadersBuilder.addUpstreamHttpProtocol(UpstreamHttpProtocol.HTTP2).build(); | ||
|
|
||
| final CountDownLatch latch = new CountDownLatch(1); | ||
| final AtomicReference<Stream> stream = new AtomicReference<>(); | ||
| final AtomicInteger bufferSent = new AtomicInteger(0); | ||
|
|
||
| // Loop 100,000 times which should be long enough to wait for the server's | ||
| // response headers to arrive. | ||
| final int numWrites = 100000; | ||
| stream.set( | ||
| engine.streamClient() | ||
| .newStreamPrototype() | ||
| .setExplicitFlowControl(true) | ||
| .setOnSendWindowAvailable((streamIntel -> { | ||
| ByteBuffer bf = ByteBuffer.allocateDirect(1); | ||
| bf.put((byte)'a'); | ||
| if (bufferSent.incrementAndGet() == numWrites) { | ||
| stream.get().close(bf); | ||
| } else { | ||
| stream.get().sendData(bf); | ||
| } | ||
| return null; | ||
| })) | ||
| .setOnResponseHeaders((responseHeaders, endStream, ignored) -> { | ||
| // This was getting executed, even in the initial test, but only | ||
| // after all the data was sent. With the fix, this should happen | ||
| // before all the data is sent which is checked in the assert | ||
| // below. | ||
| stream.get().cancel(); | ||
| return null; | ||
| }) | ||
| .setOnCancel((ignored) -> { | ||
| latch.countDown(); | ||
| return null; | ||
| }) | ||
| .start(Runnable::run) // direct executor - all the logic runs on the EM Network Thread. | ||
| .sendHeaders(requestHeaders, false)); | ||
| ByteBuffer bf = ByteBuffer.allocateDirect(1); | ||
| bf.put((byte)'a'); | ||
| stream.get().sendData(bf); | ||
|
|
||
| latch.await(); | ||
|
|
||
| assertThat(bufferSent.get()).isNotEqualTo(numWrites); | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't remember offhand: do we need to create a new one each time, or only the one time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice. That seems to work. Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apparently it didn't work - can we call out why we always need to create in a comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Done.