@@ -49,6 +49,7 @@ static final class MergeSubscriber<T> extends Subscriber<Observable<? extends T>
49
49
final Subscriber <T > actual ;
50
50
final CompositeSubscription childrenSubscriptions ;
51
51
volatile int wip ;
52
+ volatile boolean completed ;
52
53
@ SuppressWarnings ("rawtypes" )
53
54
static final AtomicIntegerFieldUpdater <MergeSubscriber > WIP_UPDATER
54
55
= AtomicIntegerFieldUpdater .newUpdater (MergeSubscriber .class , "wip" );
@@ -57,7 +58,6 @@ public MergeSubscriber(Subscriber<T> actual, CompositeSubscription childrenSubsc
57
58
super (actual );
58
59
this .actual = actual ;
59
60
this .childrenSubscriptions = childrenSubscriptions ;
60
- this .wip = 1 ;
61
61
}
62
62
63
63
@ Override
@@ -76,11 +76,20 @@ public void onError(Throwable e) {
76
76
77
77
@ Override
78
78
public void onCompleted () {
79
- if (WIP_UPDATER .decrementAndGet (this ) == 0 ) {
79
+ completed = true ;
80
+ if (wip == 0 ) {
80
81
actual .onCompleted ();
81
82
}
82
83
}
83
-
84
+ void completeInner (InnerSubscriber <T > s ) {
85
+ try {
86
+ if (WIP_UPDATER .decrementAndGet (this ) == 0 && completed ) {
87
+ actual .onCompleted ();
88
+ }
89
+ } finally {
90
+ childrenSubscriptions .remove (s );
91
+ }
92
+ }
84
93
}
85
94
static final class InnerSubscriber <T > extends Subscriber <T > {
86
95
final Subscriber <? super T > actual ;
@@ -110,11 +119,7 @@ public void onError(Throwable e) {
110
119
@ Override
111
120
public void onCompleted () {
112
121
if (ONCE_UPDATER .compareAndSet (this , 0 , 1 )) {
113
- try {
114
- parent .onCompleted ();
115
- } finally {
116
- parent .childrenSubscriptions .remove (this );
117
- }
122
+ parent .completeInner (this );
118
123
}
119
124
}
120
125
0 commit comments