Skip to content

Commit 5fe7d1d

Browse files
committed
Moved unsubscribe out the gate
1 parent 8e5e3e1 commit 5fe7d1d

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

rxjava-core/src/main/java/rx/operators/OperationSwitch.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ public void onNext(Observable<? extends T> args) {
9696
this.hasLatest = true;
9797
}
9898

99-
final SafeObservableSubscription sub;
100-
sub = new SafeObservableSubscription();
99+
final SafeObservableSubscription sub = new SafeObservableSubscription();
101100
sub.wrap(args.subscribe(new Observer<T>() {
102101
@Override
103102
public void onNext(T args) {
@@ -110,28 +109,36 @@ public void onNext(T args) {
110109

111110
@Override
112111
public void onError(Throwable e) {
112+
sub.unsubscribe();
113+
SafeObservableSubscription s = null;
113114
synchronized (gate) {
114-
sub.unsubscribe();
115115
if (latest == id) {
116116
SwitchObserver.this.observer.onError(e);
117-
SwitchObserver.this.parent.unsubscribe();
117+
s = SwitchObserver.this.parent;
118118
}
119119
}
120+
if(s != null) {
121+
s.unsubscribe();
122+
}
120123
}
121124

122125
@Override
123126
public void onCompleted() {
127+
sub.unsubscribe();
128+
SafeObservableSubscription s = null;
124129
synchronized (gate) {
125-
sub.unsubscribe();
126130
if (latest == id) {
127131
SwitchObserver.this.hasLatest = false;
128132

129133
if (stopped) {
130134
SwitchObserver.this.observer.onCompleted();
131-
SwitchObserver.this.parent.unsubscribe();
135+
s = SwitchObserver.this.parent;
132136
}
133137
}
134138
}
139+
if(s != null) {
140+
s.unsubscribe();
141+
}
135142
}
136143

137144
}));
@@ -150,13 +157,17 @@ public void onError(Throwable e) {
150157

151158
@Override
152159
public void onCompleted() {
160+
SafeObservableSubscription s = null;
153161
synchronized (gate) {
154162
this.stopped = true;
155163
if (!this.hasLatest) {
156164
this.observer.onCompleted();
157-
this.parent.unsubscribe();
165+
s = this.parent;
158166
}
159167
}
168+
if(s != null) {
169+
s.unsubscribe();
170+
}
160171
}
161172

162173
}

0 commit comments

Comments
 (0)