CompositeDisposable.clear 导致 OkHttp 抛出 java.lang.IllegalStateException: Unbalanced enter/exit
Posted
技术标签:
【中文标题】CompositeDisposable.clear 导致 OkHttp 抛出 java.lang.IllegalStateException: Unbalanced enter/exit【英文标题】:CompositeDisposable.clear causes OkHttp to throw java.lang.IllegalStateException: Unbalanced enter/exit 【发布时间】:2018-09-09 21:32:17 【问题描述】:所以我有一个使用 OkHttp 的简单 http 请求。我在 android 上使用 RxJava 执行此操作。我将此 RxJava 调用添加到 CompositeDisposable
,然后在 onStop
上清除。由于某种原因触发了下面的这个异常。我有点不确定如何解决它。
Caused by java.lang.IllegalStateException: Unbalanced enter/exit
at okio.AsyncTimeout.enter(AsyncTimeout.java:73)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:235)
at okio.RealBufferedSource.read(RealBufferedSource.java:47)
at okhttp3.internal.http1.Http1Codec$AbstractSource.read(Http1Codec.java:363)
at okhttp3.internal.http1.Http1Codec$FixedLengthSource.read(Http1Codec.java:407)
at okio.RealBufferedSource.read(RealBufferedSource.java:47)
at okhttp3.internal.cache.CacheInterceptor$1.read(CacheInterceptor.java:174)
at okhttp3.internal.Util.skipAll(Util.java:175)
at okhttp3.internal.Util.discard(Util.java:157)
at okhttp3.internal.cache.CacheInterceptor$1.close(CacheInterceptor.java:202)
at okio.RealBufferedSource.close(RealBufferedSource.java:469)
at okio.RealBufferedSource$1.close(RealBufferedSource.java:453)
at java.nio.channels.Channels$ReadableByteChannelImpl.implCloseChannel(Channels.java:255)
at java.nio.channels.spi.AbstractInterruptibleChannel$1.interrupt(AbstractInterruptibleChannel.java:166)
at java.lang.Thread.interrupt(Thread.java:957)
at java.util.concurrent.FutureTask.cancel(FutureTask.java:146)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.cancel(ScheduledThreadPoolExecutor.java:258)
at io.reactivex.internal.schedulers.ScheduledRunnable.dispose(ScheduledRunnable.java:107)
at io.reactivex.disposables.CompositeDisposable.dispose(CompositeDisposable.java:217)
at io.reactivex.disposables.CompositeDisposable.dispose(CompositeDisposable.java:80)
at io.reactivex.internal.schedulers.ioscheduler$EventLoopWorker.dispose(IoScheduler.java:210)
at io.reactivex.Scheduler$DisposeTask.dispose(Scheduler.java:464)
at io.reactivex.internal.disposables.DisposableHelper.dispose(DisposableHelper.java:125)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.dispose(ObservableSubscribeOn.java:74)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.dispose(ObservableObserveOn.java:146)
at io.reactivex.internal.disposables.DisposableHelper.dispose(DisposableHelper.java:125)
at io.reactivex.observers.DisposableObserver.dispose(DisposableObserver.java:91)
at io.reactivex.disposables.CompositeDisposable.dispose(CompositeDisposable.java:217)
at io.reactivex.disposables.CompositeDisposable.clear(CompositeDisposable.java:183)
at MyActivity.onStop(MyActivity.java:320)
at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1297)
at android.app.Activity.performStop(Activity.java:7159)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4625)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4691)
at android.app.ActivityThread.-wrap7(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6732)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
代码基本上是这样的:
Observable<Stuff> observable = Observable.create(new ObservableOnSubscribe<Stuff>()
@Override
public void subscribe(@NonNull ObservableEmitter<Stuff> e) throws Exception
//do OkHttp stuff, call onNext(stuff).
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
DisposableObserver<Stuff> disposableObserver = observable
.subscribeWith(new DisposableObserver<Stuff>() ......);
disposables.add(disposableObserver);
【问题讨论】:
【参考方案1】:某些 Android VM 中存在与新 D8 编译器交互不佳的错误。
https://github.com/square/okhttp/issues/3641#issuecomment-370717021
Google 最近改进了 D8 以避免触发该错误。
【讨论】:
哇哦,没想到。所以还要等Android Studio 3.2? 我仍然在 AS 3.1.3 上使用最新的 D8 看到这个问题,它应该已经修复了吗?以上是关于CompositeDisposable.clear 导致 OkHttp 抛出 java.lang.IllegalStateException: Unbalanced enter/exit的主要内容,如果未能解决你的问题,请参考以下文章