Set interrupted flag when throwing InterruptedIOE

This avoids the interrupted flag being lost if the exception
is handled as an IOException.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=195366244
This commit is contained in:
andrewlewis 2018-05-03 21:21:50 -07:00 committed by Oliver Woodman
parent d4d1fd64b3
commit d5034ca889
4 changed files with 14 additions and 11 deletions

View File

@ -280,6 +280,7 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
new SocketTimeoutException(), dataSpec, getStatus(currentUrlRequest)); new SocketTimeoutException(), dataSpec, getStatus(currentUrlRequest));
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new OpenException(new InterruptedIOException(e), dataSpec, Status.INVALID); throw new OpenException(new InterruptedIOException(e), dataSpec, Status.INVALID);
} }
@ -352,17 +353,18 @@ public class CronetDataSource extends UrlRequest.Callback implements HttpDataSou
if (!operation.block(readTimeoutMs)) { if (!operation.block(readTimeoutMs)) {
throw new SocketTimeoutException(); throw new SocketTimeoutException();
} }
} catch (InterruptedException | SocketTimeoutException e) { } catch (InterruptedException e) {
// If we're timing out or getting interrupted, the operation is still ongoing. // The operation is ongoing so replace readBuffer to avoid it being written to by this
// So we'll need to replace readBuffer to avoid the possibility of it being written to by // operation during a subsequent request.
// this operation during a subsequent request.
readBuffer = null; readBuffer = null;
Thread.currentThread().interrupt();
throw new HttpDataSourceException( throw new HttpDataSourceException(
e instanceof InterruptedException new InterruptedIOException(e), currentDataSpec, HttpDataSourceException.TYPE_READ);
? new InterruptedIOException((InterruptedException) e) } catch (SocketTimeoutException e) {
: (SocketTimeoutException) e, // The operation is ongoing so replace readBuffer to avoid it being written to by this
currentDataSpec, // operation during a subsequent request.
HttpDataSourceException.TYPE_READ); readBuffer = null;
throw new HttpDataSourceException(e, currentDataSpec, HttpDataSourceException.TYPE_READ);
} }
if (exception != null) { if (exception != null) {

View File

@ -325,7 +325,7 @@ public class OkHttpDataSource implements HttpDataSource {
while (bytesSkipped != bytesToSkip) { while (bytesSkipped != bytesToSkip) {
int readLength = (int) Math.min(bytesToSkip - bytesSkipped, skipBuffer.length); int readLength = (int) Math.min(bytesToSkip - bytesSkipped, skipBuffer.length);
int read = responseByteStream.read(skipBuffer, 0, readLength); int read = responseByteStream.read(skipBuffer, 0, readLength);
if (Thread.interrupted()) { if (Thread.currentThread().isInterrupted()) {
throw new InterruptedIOException(); throw new InterruptedIOException();
} }
if (read == -1) { if (read == -1) {

View File

@ -526,7 +526,7 @@ public class DefaultHttpDataSource implements HttpDataSource {
while (bytesSkipped != bytesToSkip) { while (bytesSkipped != bytesToSkip) {
int readLength = (int) Math.min(bytesToSkip - bytesSkipped, skipBuffer.length); int readLength = (int) Math.min(bytesToSkip - bytesSkipped, skipBuffer.length);
int read = inputStream.read(skipBuffer, 0, readLength); int read = inputStream.read(skipBuffer, 0, readLength);
if (Thread.interrupted()) { if (Thread.currentThread().isInterrupted()) {
throw new InterruptedIOException(); throw new InterruptedIOException();
} }
if (read == -1) { if (read == -1) {

View File

@ -326,6 +326,7 @@ public final class CacheDataSource implements DataSource {
try { try {
nextSpan = cache.startReadWrite(key, readPosition); nextSpan = cache.startReadWrite(key, readPosition);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new InterruptedIOException(); throw new InterruptedIOException();
} }
} else { } else {