-
Notifications
You must be signed in to change notification settings - Fork 226
Fix a racing condition when subscribing to a completed ScopeProvider. #135
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,15 +46,20 @@ final class AutoDisposingObserverImpl<T> implements AutoDisposingObserver<T> { | |
| @Override public void onSuccess(Object o) { | ||
| callMainSubscribeIfNecessary(d); | ||
| AutoDisposingObserverImpl.this.dispose(); | ||
| lifecycleDisposable.lazySet(AutoDisposableHelper.DISPOSED); | ||
| } | ||
|
|
||
| @Override public void onError(Throwable e) { | ||
| callMainSubscribeIfNecessary(d); | ||
| AutoDisposingObserverImpl.this.onError(e); | ||
| lifecycleDisposable.lazySet(AutoDisposableHelper.DISPOSED); | ||
| mainDisposable.lazySet(AutoDisposableHelper.DISPOSED); | ||
| } | ||
|
|
||
| @Override public void onComplete() { | ||
| callMainSubscribeIfNecessary(d); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When subscribing to a completed ScopeProvider, callMainSubscribeIfNecessary() might be called here before the main source is subscribed, which triggers the DoubleSubscriptionsException. |
||
| lifecycleDisposable.lazySet(AutoDisposableHelper.DISPOSED); | ||
| mainDisposable.lazySet(AutoDisposableHelper.DISPOSED); | ||
| // Noop - we're unbound now | ||
| } | ||
| }), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,22 @@ | |
|
|
||
| package com.uber.autodispose; | ||
|
|
||
| import com.google.common.truth.BooleanSubject; | ||
| import com.uber.autodispose.observers.AutoDisposingObserver; | ||
| import com.uber.autodispose.test.RecordingObserver; | ||
|
|
||
| import org.junit.After; | ||
| import org.junit.Test; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import io.reactivex.Observable; | ||
| import io.reactivex.ObservableEmitter; | ||
| import io.reactivex.ObservableOnSubscribe; | ||
| import io.reactivex.Observer; | ||
| import io.reactivex.disposables.Disposable; | ||
| import io.reactivex.exceptions.ProtocolViolationException; | ||
| import io.reactivex.functions.BiFunction; | ||
| import io.reactivex.functions.Cancellable; | ||
| import io.reactivex.functions.Consumer; | ||
|
|
@@ -32,10 +41,6 @@ | |
| import io.reactivex.subjects.BehaviorSubject; | ||
| import io.reactivex.subjects.MaybeSubject; | ||
| import io.reactivex.subjects.PublishSubject; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.junit.After; | ||
| import org.junit.Test; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
|
|
@@ -49,6 +54,7 @@ public class AutoDisposeObserverTest { | |
|
|
||
| @After public void resetPlugins() { | ||
| AutoDisposePlugins.reset(); | ||
| RxJavaPlugins.reset(); | ||
| } | ||
|
|
||
| @Test public void autoDispose_withMaybe_normal() { | ||
|
|
@@ -171,6 +177,24 @@ public class AutoDisposeObserverTest { | |
| assertThat(lifecycle.hasObservers()).isFalse(); | ||
| } | ||
|
|
||
| @Test public void autoDispose_withScopeProviderCompleted_shouldNotReportDoubleSubscriptions() { | ||
| RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { | ||
| @Override | ||
| public void accept(Throwable throwable) throws Exception { | ||
| assertThat(throwable instanceof ProtocolViolationException).isFalse(); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case fails in master as:
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite understand this test, as the It also won't fail if this callback isn't hit, let's make it explicit somehow that we expect this callback to be hit |
||
| } | ||
| }); | ||
| TestObserver<Integer> o = new TestObserver<>(); | ||
| PublishSubject<Integer> source = PublishSubject.create(); | ||
| MaybeSubject<Integer> scope = MaybeSubject.create(); | ||
| scope.onComplete(); | ||
| ScopeProvider scopeProvider = TestUtil.makeProvider(scope); | ||
| source.to(AutoDispose.with(scopeProvider).<Integer>forObservable()) | ||
| .subscribe(o); | ||
| o.assertNoValues(); | ||
| o.assertNoErrors(); | ||
| } | ||
|
|
||
| @Test public void autoDispose_withProvider_withoutStartingLifecycle_shouldFail() { | ||
| BehaviorSubject<Integer> lifecycle = BehaviorSubject.create(); | ||
| RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be changed to
AutoDisposableHelper.dispose(mainDisposable)