square / okhttp

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.
https://square.github.io/okhttp/
Apache License 2.0
45.83k stars 9.16k forks source link

java.io.IOException: canceled due to java.lang.ArrayIndexOutOfBoundsException: length=8192; index=-6 at okio.Buffer.indexOf #8080

Closed odbol closed 12 months ago

odbol commented 12 months ago

Similar to https://github.com/square/okhttp/issues/3938 and https://github.com/square/okhttp/issues/6280:

I don't even understand how index could be negative at that point. There seems to be tons of bounds checking happening at all the entrypoints, so perhaps there's some check that is wrong, or the long overflowing into an int or something?

Error retreiving location (Ask Studio Bot)
                                                                                                    java.io.IOException: canceled due to java.lang.ArrayIndexOutOfBoundsException: length=8192; index=-6
                                                                                                        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:185)
                                                                                                        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
                                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
                                                                                                        at java.lang.Thread.run(Thread.java:1012)
                                                                                                        Suppressed: java.lang.ArrayIndexOutOfBoundsException: length=8192; index=-6
                                                                                                            at okio.Buffer.indexOf(Buffer.java:1466)
                                                                                                            at okio.RealBufferedSource.indexOf(RealBufferedSource.java:352)
                                                                                                            at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:230)
                                                                                                            at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:224)
                                                                                                            at okhttp3.internal.cache.DiskLruCache.readJournal(DiskLruCache.java:294)
                                                                                                            at okhttp3.internal.cache.DiskLruCache.initialize(DiskLruCache.java:228)
                                                                                                            at okhttp3.internal.cache.DiskLruCache.get(DiskLruCache.java:430)
                                                                                                            at okhttp3.Cache.get(Cache.java:198)
                                                                                                            at okhttp3.Cache$1.get(Cache.java:145)
                                                                                                            at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:55)
                                                                                                            at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
                                                                                                            at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
                                                                                                            at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
                                                                                                            at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
                                                                                                            at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
                                                                                                            at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
                                                                                                            at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
                                                                                                            at com.odbol.wear.airquality.purpleair.CachingClient.createClient$lambda$1(CachingClient.kt:61)
                                                                                                            at com.odbol.wear.airquality.purpleair.CachingClient.$r8$lambda$dieXNzrD9l4hvF6JQkAhyOPAbjE(Unknown Source:0)
                                                                                                            at com.odbol.wear.airquality.purpleair.CachingClient$$ExternalSyntheticLambda1.intercept(Unknown Source:0)

Source code is here: https://github.com/odbol/air-quality-complication/blob/master/app/src/main/java/com/odbol/wear/airquality/purpleair/CachingClient.kt#L61

Happens running on a Pixel Watch 1.

yschimke commented 12 months ago

You should only have a single cache pointing at a directory. Generally this type of issue is most likely threading issues by either multiple threads, or multiple client instances.

You should lift your OkHttpClient and Cache up to a singleton.

But if you can give me a set of steps to reproduce the crash, I can try running your Wear app to confirm.

yschimke commented 12 months ago

I tried running the app, but even with an API key, it's taking 3 seconds per request and timing out. I'm almost certain, it's just having two clients pointing at the same Cache directory.

You should lift that up to the Application.

Re-open if it is still happening with a single Cache.

odbol commented 12 months ago

Yep, making the cache a singleton worked. Thanks Yuri! (Didn't know you frequented this repo as well haha)

yschimke commented 12 months ago

My OkHttp involvement predates Wear :) I'm not stalking Wear developers across the internet.