Skip to content

Commit 1fa8703

Browse files
committed
1.x: Fix the observer array becoming visible too early and causing NPE
1 parent 182833e commit 1fa8703

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/main/java/rx/internal/operators/OperatorZip.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ public void request(long n) {
177177

178178
}
179179

180-
private static final class Zip<R> extends AtomicLong {
180+
static final class Zip<R> extends AtomicLong {
181+
/** */
182+
private static final long serialVersionUID = 5995274816189928317L;
183+
181184
final Observer<? super R> child;
182185
private final FuncN<? extends R> zipFunction;
183186
private final CompositeSubscription childSubscription = new CompositeSubscription();
@@ -186,7 +189,7 @@ private static final class Zip<R> extends AtomicLong {
186189
int emitted = 0; // not volatile/synchronized as accessed inside COUNTER_UPDATER block
187190

188191
/* initialized when started in `start` */
189-
private Object[] observers;
192+
private volatile Object[] observers;
190193
private AtomicLong requested;
191194

192195
public Zip(final Subscriber<? super R> child, FuncN<? extends R> zipFunction) {
@@ -197,14 +200,16 @@ public Zip(final Subscriber<? super R> child, FuncN<? extends R> zipFunction) {
197200

198201
@SuppressWarnings("unchecked")
199202
public void start(@SuppressWarnings("rawtypes") Observable[] os, AtomicLong requested) {
200-
observers = new Object[os.length];
201-
this.requested = requested;
203+
Object[] observers = new Object[os.length];
202204
for (int i = 0; i < os.length; i++) {
203205
InnerSubscriber io = new InnerSubscriber();
204206
observers[i] = io;
205207
childSubscription.add(io);
206208
}
207-
209+
210+
this.requested = requested;
211+
this.observers = observers; // full memory barrier: release all above
212+
208213
for (int i = 0; i < os.length; i++) {
209214
os[i].unsafeSubscribe((InnerSubscriber) observers[i]);
210215
}

0 commit comments

Comments
 (0)