Skip to content

Hardened both cache() and replay() against child-thrown exceptions. #3088

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import rx.*;
import rx.functions.Action1;
import rx.observables.ConnectableObservable;
import rx.observers.Subscribers;

/**
* Wraps a ConnectableObservable and calls its connect() method once
Expand All @@ -47,7 +48,7 @@ public OnSubscribeAutoConnect(ConnectableObservable<? extends T> source,
}
@Override
public void call(Subscriber<? super T> child) {
source.unsafeSubscribe(child);
source.unsafeSubscribe(Subscribers.wrap(child));
if (clients.incrementAndGet() == numberOfSubscribers) {
source.connect(connection);
}
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/rx/internal/operators/OperatorReplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import rx.*;
import rx.Observable;
import rx.exceptions.Exceptions;
import rx.exceptions.OnErrorThrowable;
import rx.functions.*;
import rx.observables.ConnectableObservable;
import rx.schedulers.Timestamped;
Expand Down Expand Up @@ -813,7 +814,16 @@ public void replay(InnerProducer<T> output) {

while (r != 0L && destIndex < sourceIndex) {
Object o = get(destIndex);
if (nl.accept(output.child, o)) {
try {
if (nl.accept(output.child, o)) {
return;
}
} catch (Throwable err) {
Exceptions.throwIfFatal(err);
output.unsubscribe();
if (!nl.isError(o) && !nl.isCompleted(o)) {
output.child.onError(OnErrorThrowable.addValueAsLastCause(err, nl.getValue(o)));
}
return;
}
if (output.isUnsubscribed()) {
Expand Down Expand Up @@ -969,8 +979,18 @@ public final void replay(InnerProducer<T> output) {
Node v = node.get();
if (v != null) {
Object o = leaveTransform(v.value);
if (nl.accept(output.child, o)) {
try {
if (nl.accept(output.child, o)) {
output.index = null;
return;
}
} catch (Throwable err) {
output.index = null;
Exceptions.throwIfFatal(err);
output.unsubscribe();
if (!nl.isError(o) && !nl.isCompleted(o)) {
output.child.onError(OnErrorThrowable.addValueAsLastCause(err, nl.getValue(o)));
}
return;
}
e++;
Expand Down
92 changes: 61 additions & 31 deletions src/main/java/rx/internal/util/CachedObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.concurrent.atomic.*;

import rx.*;
import rx.exceptions.Exceptions;
import rx.exceptions.OnErrorThrowable;
import rx.internal.operators.NotificationLite;
import rx.subscriptions.SerialSubscription;

Expand Down Expand Up @@ -170,48 +172,51 @@ public void removeProducer(ReplayProducer<T> p) {
* Make sure this is called only once.
*/
public void connect() {
connection.set(source.subscribe(this));
Subscriber<T> subscriber = new Subscriber<T>() {
@Override
public void onNext(T t) {
CacheState.this.onNext(t);
}
@Override
public void onError(Throwable e) {
CacheState.this.onError(e);
}
@Override
public void onCompleted() {
CacheState.this.onCompleted();
}
};
connection.set(subscriber);
source.unsafeSubscribe(subscriber);
isConnected = true;
}
@Override
public void onNext(T t) {
Object o = nl.next(t);
synchronized (this) {
if (!sourceDone) {
add(o);
} else {
return;
}
if (!sourceDone) {
Object o = nl.next(t);
add(o);
dispatch();
}
dispatch();
}
@Override
public void onError(Throwable e) {
Object o = nl.error(e);
synchronized (this) {
if (!sourceDone) {
sourceDone = true;
add(o);
} else {
return;
}
if (!sourceDone) {
sourceDone = true;
Object o = nl.error(e);
add(o);
connection.unsubscribe();
dispatch();
}
connection.unsubscribe();
dispatch();
}
@Override
public void onCompleted() {
Object o = nl.completed();
synchronized (this) {
if (!sourceDone) {
sourceDone = true;
add(o);
} else {
return;
}
if (!sourceDone) {
sourceDone = true;
Object o = nl.completed();
add(o);
connection.unsubscribe();
dispatch();
}
connection.unsubscribe();
dispatch();
}
/**
* Signals all known children there is work to do.
Expand Down Expand Up @@ -352,6 +357,12 @@ public void replay() {
for (;;) {

long r = get();

if (r < 0L) {
skipFinal = true;
return;
}

// read the size, if it is non-zero, we can safely read the head and
// read values up to the given absolute index
int s = state.size();
Expand Down Expand Up @@ -385,16 +396,30 @@ public void replay() {
if (r > 0) {
int valuesProduced = 0;

while (j < s && r > 0 && !child.isUnsubscribed()) {
while (j < s && r > 0) {
if (child.isUnsubscribed()) {
skipFinal = true;
return;
}
if (k == n) {
b = (Object[])b[n];
k = 0;
}
Object o = b[k];

if (nl.accept(child, o)) {
try {
if (nl.accept(child, o)) {
skipFinal = true;
unsubscribe();
return;
}
} catch (Throwable err) {
Exceptions.throwIfFatal(err);
skipFinal = true;
unsubscribe();
if (!nl.isError(o) && !nl.isCompleted(o)) {
child.onError(OnErrorThrowable.addValueAsLastCause(err, nl.getValue(o)));
}
return;
}

Expand All @@ -404,6 +429,11 @@ public void replay() {
valuesProduced++;
}

if (child.isUnsubscribed()) {
skipFinal = true;
return;
}

index = j;
currentIndexInBuffer = k;
currentBuffer = b;
Expand Down
Loading