Skip to content

1.x: Fix zip() - observer array becoming visible too early and causing NPE #3688

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

Merged
merged 1 commit into from
Feb 10, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/main/java/rx/internal/operators/OperatorZip.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ public void request(long n) {

}

private static final class Zip<R> extends AtomicLong {
static final class Zip<R> extends AtomicLong {
/** */
private static final long serialVersionUID = 5995274816189928317L;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really need it? Also, javadoc comment above does not make sense

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less javac warnings.


final Observer<? super R> child;
private final FuncN<? extends R> zipFunction;
private final CompositeSubscription childSubscription = new CompositeSubscription();
Expand All @@ -186,7 +189,7 @@ private static final class Zip<R> extends AtomicLong {
int emitted = 0; // not volatile/synchronized as accessed inside COUNTER_UPDATER block

/* initialized when started in `start` */
private Object[] observers;
private volatile Object[] subscribers;
private AtomicLong requested;

public Zip(final Subscriber<? super R> child, FuncN<? extends R> zipFunction) {
Expand All @@ -197,16 +200,18 @@ public Zip(final Subscriber<? super R> child, FuncN<? extends R> zipFunction) {

@SuppressWarnings("unchecked")
public void start(@SuppressWarnings("rawtypes") Observable[] os, AtomicLong requested) {
observers = new Object[os.length];
this.requested = requested;
final Object[] subscribers = new Object[os.length];
for (int i = 0; i < os.length; i++) {
InnerSubscriber io = new InnerSubscriber();
observers[i] = io;
subscribers[i] = io;
childSubscription.add(io);
}


this.requested = requested;
this.subscribers = subscribers; // full memory barrier: release all above

for (int i = 0; i < os.length; i++) {
os[i].unsafeSubscribe((InnerSubscriber) observers[i]);
os[i].unsafeSubscribe((InnerSubscriber) subscribers[i]);
}
}

Expand All @@ -219,13 +224,13 @@ public void start(@SuppressWarnings("rawtypes") Observable[] os, AtomicLong requ
*/
@SuppressWarnings("unchecked")
void tick() {
final Object[] observers = this.observers;
if (observers == null) {
final Object[] subscribers = this.subscribers;
if (subscribers == null) {
// nothing yet to do (initial request from Producer)
return;
}
if (getAndIncrement() == 0) {
final int length = observers.length;
final int length = subscribers.length;
final Observer<? super R> child = this.child;
final AtomicLong requested = this.requested;
do {
Expand All @@ -234,7 +239,7 @@ void tick() {
final Object[] vs = new Object[length];
boolean allHaveValues = true;
for (int i = 0; i < length; i++) {
RxRingBuffer buffer = ((InnerSubscriber) observers[i]).items;
RxRingBuffer buffer = ((InnerSubscriber) subscribers[i]).items;
Object n = buffer.peek();

if (n == null) {
Expand Down Expand Up @@ -265,7 +270,7 @@ void tick() {
return;
}
// now remove them
for (Object obj : observers) {
for (Object obj : subscribers) {
RxRingBuffer buffer = ((InnerSubscriber) obj).items;
buffer.poll();
// eagerly check if the next item on this queue is an onComplete
Expand All @@ -278,7 +283,7 @@ void tick() {
}
}
if (emitted > THRESHOLD) {
for (Object obj : observers) {
for (Object obj : subscribers) {
((InnerSubscriber) obj).requestMore(emitted);
}
emitted = 0;
Expand Down