Skip to content

Commit

Permalink
Recover from a previous allocated connection with noNewStreams.
Browse files Browse the repository at this point in the history
This is an edge case that can occur with HTTP/2. Since multiple requests
use the same connection, it's possible for one request to flag the
connection as bad during a follow-up request.

#3521
  • Loading branch information
dave-r12 committed Aug 15, 2017
1 parent 2e934cf commit 58928f5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;

import javax.net.ssl.HostnameVerifier;
import okhttp3.Cache;
import okhttp3.Call;
Expand All @@ -46,6 +45,7 @@
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.Route;
import okhttp3.TestUtil;
import okhttp3.internal.DoubleInetAddressDns;
import okhttp3.internal.RecordingOkAuthenticator;
Expand Down Expand Up @@ -736,6 +736,64 @@ private void noRecoveryFromErrorWithRetryDisabled(ErrorCode errorCode) throws Ex
}
}

@Test public void recoverFromConnectionNoNewStreamsOnFollowUp() throws InterruptedException {
server.enqueue(new MockResponse()
.setResponseCode(401));
server.enqueue(new MockResponse()
.setSocketPolicy(SocketPolicy.RESET_STREAM_AT_START)
.setHttp2ErrorCode(ErrorCode.CANCEL.httpCode));
server.enqueue(new MockResponse()
.setBody("DEF"));
server.enqueue(new MockResponse()
.setBody("ABC"));

final CountDownLatch latch = new CountDownLatch(1);
final BlockingQueue<String> responses = new SynchronousQueue<>();
okhttp3.Authenticator authenticator = new okhttp3.Authenticator() {
@Override public Request authenticate(Route route, Response response) throws IOException {
responses.offer(response.body().string());
try {
latch.await();
} catch (InterruptedException e) {
throw new AssertionError();
}
return response.request();
}
};

OkHttpClient blockingAuthClient = client.newBuilder()
.authenticator(authenticator)
.build();

Callback callback = new Callback() {
@Override public void onFailure(Call call, IOException e) {
fail();
}

@Override public void onResponse(Call call, Response response) throws IOException {
responses.offer(response.body().string());
}
};

// Make the first request waiting until we get our auth challenge.
Request request = new Request.Builder()
.url(server.url("/"))
.build();
blockingAuthClient.newCall(request).enqueue(callback);
String response1 = responses.take();
assertEquals("", response1);

// Now make the second request which will cause the HTTP/2 connection to be flagged as bad.
client.newCall(request).enqueue(callback);
String response2 = responses.take();
assertEquals("DEF", response2);

// Let the first request proceed.
latch.countDown();
String response3 = responses.take();
assertEquals("ABC", response3);
}

@Test public void nonAsciiResponseHeader() throws Exception {
server.enqueue(new MockResponse()
.addHeaderLenient("Alpha", "α")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ private RealConnection findConnection(int connectTimeout, int readTimeout, int w
if (canceled) throw new IOException("Canceled");

// Attempt to use an already-allocated connection.
RealConnection allocatedConnection = this.connection;
if (allocatedConnection != null && !allocatedConnection.noNewStreams) {
RealConnection allocatedConnection = previousAllocatedConnection();
if (allocatedConnection != null) {
return allocatedConnection;
}

Expand Down Expand Up @@ -257,6 +257,29 @@ private RealConnection findConnection(int connectTimeout, int readTimeout, int w
return result;
}

/**
* Returns the previous allocated connection or null if there is no previous allocated connection.
* We will have a previous allocated connection in the case of follow-up requests. With HTTP/2
* multiple requests share the same connection so it's possible that our connection is flagged to
* disallow new streams during a follow-up request.
*/
private RealConnection previousAllocatedConnection() {
assert (Thread.holdsLock(connectionPool));

RealConnection allocatedConnection = this.connection;
if (allocatedConnection == null) return null;

if (allocatedConnection.noNewStreams) {
// Our connection was flagged to disallow new streams. This could happen in HTTP/2 when
// multiple requests use the same connection. Discard it.
deallocate(false, true, true);
return null;
}

// We're good!
return allocatedConnection;
}

public void streamFinished(boolean noNewStreams, HttpCodec codec) {
Socket socket;
Connection releasedConnection;
Expand Down

0 comments on commit 58928f5

Please sign in to comment.