1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-14 07:22:20 +03:00

Cancel calls on unexpected exceptions (3.12.x branch)

Closes: https://github.com/square/okhttp/issues/5151
This commit is contained in:
Jesse Wilson
2019-09-14 13:45:38 -04:00
parent 043b57006c
commit 0886def52d
2 changed files with 17 additions and 5 deletions

View File

@@ -553,13 +553,14 @@ public final class InterceptorTest {
}
/**
* When an interceptor throws an unexpected exception, asynchronous callers are left hanging. The
* When an interceptor throws an unexpected exception, asynchronous calls are canceled. The
* exception goes to the uncaught exception handler.
*/
private void interceptorThrowsRuntimeExceptionAsynchronous(boolean network) throws Exception {
final RuntimeException boom = new RuntimeException("boom!");
addInterceptor(network, new Interceptor() {
@Override public Response intercept(Chain chain) throws IOException {
throw new RuntimeException("boom!");
throw boom;
}
});
@@ -571,9 +572,13 @@ public final class InterceptorTest {
Request request = new Request.Builder()
.url(server.url("/"))
.build();
client.newCall(request).enqueue(callback);
assertEquals("boom!", executor.takeException().getMessage());
Call call = client.newCall(request);
call.enqueue(callback);
RecordedResponse recordedResponse = callback.await(server.url("/"));
assertEquals("canceled due to java.lang.RuntimeException: boom!",
recordedResponse.failure.getMessage());
assertTrue(call.isCanceled());
assertSame(boom, executor.takeException());
}
@Test public void applicationInterceptorReturnsNull() throws Exception {

View File

@@ -210,6 +210,13 @@ final class RealCall implements Call {
eventListener.callFailed(RealCall.this, e);
responseCallback.onFailure(RealCall.this, e);
}
} catch (Throwable t) {
cancel();
if (!signalledCallback) {
IOException canceledException = new IOException("canceled due to " + t);
responseCallback.onFailure(RealCall.this, canceledException);
}
throw t;
} finally {
client.dispatcher().finished(this);
}