Commit 6bf62770 by Oliver Woodman

Make sure that the process dies if a loading task throws Error.

parent 1d528b80
...@@ -99,7 +99,8 @@ public final class Loader { ...@@ -99,7 +99,8 @@ public final class Loader {
} }
private static final int MSG_END_OF_SOURCE = 0; private static final int MSG_END_OF_SOURCE = 0;
private static final int MSG_ERROR = 1; private static final int MSG_IO_EXCEPTION = 1;
private static final int MSG_FATAL_ERROR = 2;
private final ExecutorService downloadExecutorService; private final ExecutorService downloadExecutorService;
...@@ -242,20 +243,30 @@ public final class Loader { ...@@ -242,20 +243,30 @@ public final class Loader {
} }
sendEmptyMessage(MSG_END_OF_SOURCE); sendEmptyMessage(MSG_END_OF_SOURCE);
} catch (IOException e) { } catch (IOException e) {
obtainMessage(MSG_ERROR, e).sendToTarget(); obtainMessage(MSG_IO_EXCEPTION, e).sendToTarget();
} catch (InterruptedException e) { } catch (InterruptedException e) {
// The load was canceled. // The load was canceled.
Assertions.checkState(loadable.isLoadCanceled()); Assertions.checkState(loadable.isLoadCanceled());
sendEmptyMessage(MSG_END_OF_SOURCE); sendEmptyMessage(MSG_END_OF_SOURCE);
} catch (Exception e) { } catch (Exception e) {
// This should never happen, but handle it anyway. // This should never happen, but handle it anyway.
Log.e(TAG, "Unexpected exception loading stream", e);
obtainMessage(MSG_IO_EXCEPTION, new UnexpectedLoaderException(e)).sendToTarget();
} catch (Error e) {
// We'd hope that the platform would kill the process if an Error is thrown here, but the
// executor may catch the error (b/20616433). Throw it here, but also pass and throw it from
// the handler thread so that the process dies even if the executor behaves in this way.
Log.e(TAG, "Unexpected error loading stream", e); Log.e(TAG, "Unexpected error loading stream", e);
obtainMessage(MSG_ERROR, new UnexpectedLoaderException(e)).sendToTarget(); obtainMessage(MSG_FATAL_ERROR, e).sendToTarget();
throw e;
} }
} }
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
if (msg.what == MSG_FATAL_ERROR) {
throw (Error) msg.obj;
}
onFinished(); onFinished();
if (loadable.isLoadCanceled()) { if (loadable.isLoadCanceled()) {
callback.onLoadCanceled(loadable); callback.onLoadCanceled(loadable);
...@@ -265,7 +276,7 @@ public final class Loader { ...@@ -265,7 +276,7 @@ public final class Loader {
case MSG_END_OF_SOURCE: case MSG_END_OF_SOURCE:
callback.onLoadCompleted(loadable); callback.onLoadCompleted(loadable);
break; break;
case MSG_ERROR: case MSG_IO_EXCEPTION:
callback.onLoadError(loadable, (IOException) msg.obj); callback.onLoadError(loadable, (IOException) msg.obj);
break; break;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment