Skip to content
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

fix #2124 Backport and improve discard handling in UnicastProcessor #2126

Merged
merged 2 commits into from Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -154,6 +154,7 @@ public static <E> UnicastProcessor<E> create(Queue<E> queue,
volatile boolean done;
Throwable error;

boolean hasDownstream; //important to not loose the downstream too early and miss discard hook, while having relevant hasDownstreams()
volatile CoreSubscriber<? super T> actual;

volatile boolean cancelled;
Expand Down Expand Up @@ -203,6 +204,7 @@ public int getBufferSize() {
@Override
public Object scanUnsafe(Attr key) {
if (Attr.BUFFERED == key) return queue.size();
if (Attr.PREFETCH == key) return Integer.MAX_VALUE;
return super.scanUnsafe(key);
}

Expand All @@ -213,7 +215,7 @@ void doTerminate() {
}
}

void drainRegular(Subscriber<? super T> a) {
void drainRegular(CoreSubscriber<? super T> a) {
int missed = 1;

final Queue<T> q = queue;
Expand All @@ -229,7 +231,7 @@ void drainRegular(Subscriber<? super T> a) {
T t = q.poll();
boolean empty = t == null;

if (checkTerminated(d, empty, a, q)) {
if (checkTerminated(d, empty, a, q, t)) {
return;
}

Expand All @@ -243,7 +245,7 @@ void drainRegular(Subscriber<? super T> a) {
}

if (r == e) {
if (checkTerminated(done, q.isEmpty(), a, q)) {
if (checkTerminated(done, q.isEmpty(), a, q, null)) {
return;
}
}
Expand All @@ -259,16 +261,16 @@ void drainRegular(Subscriber<? super T> a) {
}
}

void drainFused(Subscriber<? super T> a) {
void drainFused(CoreSubscriber<? super T> a) {
int missed = 1;

final Queue<T> q = queue;

for (;;) {

if (cancelled) {
q.clear();
actual = null;
Operators.onDiscardQueueWithClear(q, a.currentContext(), null);
hasDownstream = false;
return;
}

Expand All @@ -277,7 +279,7 @@ void drainFused(Subscriber<? super T> a) {
a.onNext(null);

if (d) {
actual = null;
hasDownstream = false;

Throwable ex = error;
if (ex != null) {
Expand All @@ -295,15 +297,18 @@ void drainFused(Subscriber<? super T> a) {
}
}

void drain() {
void drain(@Nullable T dataSignalOfferedBeforeDrain) {
if (WIP.getAndIncrement(this) != 0) {
if (dataSignalOfferedBeforeDrain != null && cancelled) {
Operators.onDiscard(dataSignalOfferedBeforeDrain, actual.currentContext());
}
return;
}

int missed = 1;

for (;;) {
Subscriber<? super T> a = actual;
CoreSubscriber<? super T> a = actual;
if (a != null) {

if (outputFused) {
Expand All @@ -321,15 +326,16 @@ void drain() {
}
}

boolean checkTerminated(boolean d, boolean empty, Subscriber<? super T> a, Queue<T> q) {
boolean checkTerminated(boolean d, boolean empty, CoreSubscriber<? super T> a, Queue<T> q, @Nullable T t) {
if (cancelled) {
q.clear();
actual = null;
Operators.onDiscard(t, a.currentContext());
Operators.onDiscardQueueWithClear(q, a.currentContext(), null);
hasDownstream = false;
return true;
}
if (d && empty) {
Throwable e = error;
actual = null;
hasDownstream = false;
if (e != null) {
a.onError(e);
} else {
Expand Down Expand Up @@ -369,9 +375,10 @@ public void onNext(T t) {
}

if (!queue.offer(t)) {
Context ctx = actual.currentContext();
Throwable ex = Operators.onOperatorError(null,
Exceptions.failWithOverflow(), t, currentContext());
if(onOverflow != null) {
Exceptions.failWithOverflow(), t, ctx);
if (onOverflow != null) {
try {
onOverflow.accept(t);
}
Expand All @@ -380,10 +387,11 @@ public void onNext(T t) {
ex.initCause(e);
}
}
onError(Operators.onOperatorError(null, ex, t, currentContext()));
Operators.onDiscard(t, ctx);
onError(ex);
return;
}
drain();
drain(t);
}

@Override
Expand All @@ -398,7 +406,7 @@ public void onError(Throwable t) {

doTerminate();

drain();
drain(null);
}

@Override
Expand All @@ -411,20 +419,21 @@ public void onComplete() {

doTerminate();

drain();
drain(null);
}

@Override
public void subscribe(CoreSubscriber<? super T> actual) {
Objects.requireNonNull(actual, "subscribe");
if (once == 0 && ONCE.compareAndSet(this, 0, 1)) {

this.hasDownstream = true;
actual.onSubscribe(this);
this.actual = actual;
if (cancelled) {
this.actual = null;
this.hasDownstream = false;
} else {
drain();
drain(null);
}
} else {
Operators.error(actual, new IllegalStateException("UnicastProcessor " +
Expand All @@ -436,7 +445,7 @@ public void subscribe(CoreSubscriber<? super T> actual) {
public void request(long n) {
if (Operators.validate(n)) {
Operators.addCap(REQUESTED, this, n);
drain();
drain(null);
}
}

Expand All @@ -449,11 +458,9 @@ public void cancel() {

doTerminate();

if (!outputFused) {
if (WIP.getAndIncrement(this) == 0) {
queue.clear();
actual = null;
}
if (WIP.getAndIncrement(this) == 0) {
Operators.onDiscardQueueWithClear(queue, currentContext(), null);
hasDownstream = false;
}
}

Expand All @@ -475,7 +482,7 @@ public boolean isEmpty() {

@Override
public void clear() {
queue.clear();
Operators.onDiscardQueueWithClear(queue, currentContext(), null);
}

@Override
Expand Down Expand Up @@ -515,6 +522,6 @@ public long downstreamCount() {

@Override
public boolean hasDownstreams() {
return actual != null;
return hasDownstream;
}
}