Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logs uncatchable java.io.InterruptedIOException #152

Closed
musselwhizzle opened this issue Jun 3, 2015 · 4 comments
Closed

Logs uncatchable java.io.InterruptedIOException #152

musselwhizzle opened this issue Jun 3, 2015 · 4 comments

Comments

@musselwhizzle
Copy link

If you get an InputStream from Okio and interrupt it's Thread, Okio logs an uncatchable exception.

06-03 16:05:03.973 3295-3394/com.junk.sampleapp W/System.err﹕ java.io.InterruptedIOException
06-03 16:05:03.975 3295-3394/com.junk.sampleapp W/System.err﹕ at com.android.okio.Deadline.throwIfReached(Deadline.java:56)
06-03 16:05:03.975 3295-3394/com.junk.sampleapp W/System.err﹕ at com.android.okio.Okio$2.read(Okio.java:110)
06-03 16:05:03.975 3295-3394/com.junk.sampleapp W/System.err﹕ at com.android.okio.RealBufferedSource.read(RealBufferedSource.java:48)
06-03 16:05:03.975 3295-3394/com.junk.sampleapp W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:446)
06-03 16:05:03.975 3295-3394/com.junk.sampleapp W/System.err﹕ at com.android.okio.RealBufferedSource$1.read(RealBufferedSource.java:168)
06-03 16:05:03.975 3295-3394/com.junk.sampleapp W/System.err﹕ at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-03 16:05:03.975 3295-3394/com.junk.sampleapp W/System.err﹕ at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
06-03 16:05:03.975 3295-3394/com.junk.sampleapp W/System.err﹕ at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
06-03 16:05:03.975 3295-3394/com.junk.sampleapp W/System.err﹕ at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:649)

Below is sample code (Android) to reproduce

public class MainActivity extends ActionBarActivity {

private UrlConnTask task;
private boolean isLoad = true;
private Button button;
private ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    installStreamHandler();
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    button = new Button(this);
    button.setText("Start process");
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            loadAndCancel();
        }
    });
    ll.addView(button);
    ll.addView(iv = new ImageView(this));
    setContentView(ll);
}

private void installStreamHandler() {
    URL.setURLStreamHandlerFactory(new OkUrlFactory(new OkHttpClient()));
}

private void loadAndCancel() {
    if (isLoad) {
        iv.setImageBitmap(null);
        task = new UrlConnTask();
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "http://your_image_url_here.png");
    } else {
        task.cancel(true);
    }
    button.setText(isLoad ? "Cancel" : "Load");
    isLoad = !isLoad;
}


private class UrlConnTask extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... params) {
        try {
            URL u = new URL(params[0]);
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setConnectTimeout(2500);
            conn.setReadTimeout(2500);
            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setInstanceFollowRedirects(false);
            conn.connect();
            InputStream is = conn.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            return bitmap;
        } catch (InterruptedIOException e) { // uncatchable. This never reached. 
            Log.i("Main", "Caught an InterruptedIOException", e);
            return null;
        } catch (IOException e) {
            Log.i("Main", "Caught an IOException", e);
            return null;
        }
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        if (isCancelled()) return;
        Log.i("Main", "Got an image:" + (bitmap != null));
        iv.setImageBitmap(bitmap);
    }

    @Override
    protected void onCancelled() {
        Log.i("Main", "onCancelled");
        iv.setImageBitmap(null);
    }
}

}

@swankjesse
Copy link
Member

It's your code calling into BitmapFactory.decodeStream. Nothing we can do! You probably want to catch the exception into whatever is decoding your bitmaps.

@musselwhizzle
Copy link
Author

You probably want to catch the exception into whatever is decoding your bitmaps.

If you notice in the sample code and the description, the exception is never thrown but swallowed. The sample above is nice to look at if you haven't yet.

@swankjesse
Copy link
Member

Can you simplify the sample to omit Android's bitmap decoder? That isn't okio and suggests the problem lies far away from okio.

@musselwhizzle
Copy link
Author

It's your code calling into
Oh I see what you mean. It's BitmapFactory. decodeStream swallowing the error and not okhttp. Yea, nothing you can do. Thanks for looking into it and considering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants