Skip to content

Commit 0f502b2

Browse files
yschimkeicbaker
authored andcommitted
Workaround for OkHttp Interrupt issues.
Relates to square/okhttp#3146. This was from androidx/media#71. There is a draft PR https://github.com/square/okhttp/pull/7185/files which documents OkHttp's ideal handling of cancellation including interrupts. But a few key points 1) This is a target state, and OkHttp does not currently handle interrupts correctly. In the past this has been identified, and the advice is to avoid interrupts on Http threads, see discussion on square/okhttp#1902. Also an attempt at a fix here square/okhttp#7023 which wasn't in a form to land. 2) Even with this fixed, it is likely to never be optimal, because of OkHttp sharing a socket connection for multiple inflight requests. From square/okhttp#7185 ``` Thread.interrupt() is Clumsy ---------------------------- `Thread.interrupt()` is Java's built-in mechanism to cancel an in-flight `Thread`, regardless of what work it's currently performing. We recommend against using `Thread.interrupt()` with OkHttp because it may disrupt shared resources including HTTP/2 connections and cache files. In particular, calling `Thread.interrupt()` may cause unrelated threads' call to fail with an `IOException`. ``` This PR leaves the Loader/DataSource thread parked on a countdown latch, while this may seem wasteful and an additional context switch. However in practice the response isn't returned until the Http2Connection and Http2Reader have a response from the server and these means effectively parking in a `wait()` statement here https://github.com/square/okhttp/blob/9e039e94123defbfd5f11dc64ae146c46b7230eb/okhttp/src/jvmMain/kotlin/okhttp3/internal/http2/Http2Stream.kt#L140 PiperOrigin-RevId: 446652468
1 parent 3701e80 commit 0f502b2

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

extensions/okhttp/src/main/java/com/google/android/exoplayer2/ext/okhttp/OkHttpDataSource.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,27 @@
2929
import com.google.android.exoplayer2.upstream.DataSourceException;
3030
import com.google.android.exoplayer2.upstream.DataSpec;
3131
import com.google.android.exoplayer2.upstream.HttpDataSource;
32+
import com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException;
33+
import com.google.android.exoplayer2.upstream.HttpDataSource.InvalidContentTypeException;
34+
import com.google.android.exoplayer2.upstream.HttpDataSource.InvalidResponseCodeException;
3235
import com.google.android.exoplayer2.upstream.HttpUtil;
3336
import com.google.android.exoplayer2.upstream.TransferListener;
3437
import com.google.android.exoplayer2.util.Assertions;
3538
import com.google.android.exoplayer2.util.Util;
3639
import com.google.common.base.Predicate;
3740
import com.google.common.net.HttpHeaders;
41+
import com.google.common.util.concurrent.SettableFuture;
3842
import java.io.IOException;
3943
import java.io.InputStream;
4044
import java.io.InterruptedIOException;
4145
import java.util.Collections;
4246
import java.util.HashMap;
4347
import java.util.List;
4448
import java.util.Map;
49+
import java.util.concurrent.ExecutionException;
4550
import okhttp3.CacheControl;
4651
import okhttp3.Call;
52+
import okhttp3.Callback;
4753
import okhttp3.HttpUrl;
4854
import okhttp3.MediaType;
4955
import okhttp3.OkHttpClient;
@@ -281,8 +287,9 @@ public long open(DataSpec dataSpec) throws HttpDataSourceException {
281287
Request request = makeRequest(dataSpec);
282288
Response response;
283289
ResponseBody responseBody;
290+
Call call = callFactory.newCall(request);
284291
try {
285-
this.response = callFactory.newCall(request).execute();
292+
this.response = executeCall(call);
286293
response = this.response;
287294
responseBody = Assertions.checkNotNull(response.body());
288295
responseByteStream = responseBody.byteStream();
@@ -428,6 +435,35 @@ private Request makeRequest(DataSpec dataSpec) throws HttpDataSourceException {
428435
return builder.build();
429436
}
430437

438+
/**
439+
* This method is an interrupt safe replacement of OkHttp Call.execute() which can get in bad
440+
* states if interrupted while writing to the shared connection socket.
441+
*/
442+
private Response executeCall(Call call) throws IOException {
443+
SettableFuture<Response> future = SettableFuture.create();
444+
call.enqueue(
445+
new Callback() {
446+
@Override
447+
public void onFailure(Call call, IOException e) {
448+
future.setException(e);
449+
}
450+
451+
@Override
452+
public void onResponse(Call call, Response response) {
453+
future.set(response);
454+
}
455+
});
456+
457+
try {
458+
return future.get();
459+
} catch (InterruptedException e) {
460+
call.cancel();
461+
throw new InterruptedIOException();
462+
} catch (ExecutionException ee) {
463+
throw new IOException(ee);
464+
}
465+
}
466+
431467
/**
432468
* Attempts to skip the specified number of bytes in full.
433469
*

0 commit comments

Comments
 (0)