Skip to content

ensures RRRSubscriber doesn't cancel subscription on onNext #918

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
Aug 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.
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 @@ -58,6 +58,7 @@ final class RequestResponseResponderSubscriber

final RSocket handler;

boolean done;
CompositeByteBuf frames;

volatile Subscription s;
Expand Down Expand Up @@ -109,13 +110,24 @@ public void onSubscribe(Subscription subscription) {

@Override
public void onNext(@Nullable Payload p) {
if (!Operators.terminate(S, this)) {
if (this.done) {
if (p != null) {
p.release();
}
return;
}

final Subscription currentSubscription = this.s;
if (currentSubscription == Operators.cancelledSubscription()
|| !S.compareAndSet(this, currentSubscription, Operators.cancelledSubscription())) {
if (p != null) {
p.release();
}
return;
}

this.done = true;

final int streamId = this.streamId;
final UnboundedProcessor<ByteBuf> sender = this.sendProcessor;
final ByteBufAllocator allocator = this.allocator;
Expand All @@ -131,6 +143,8 @@ public void onNext(@Nullable Payload p) {
final int mtu = this.mtu;
try {
if (!isValid(mtu, this.maxFrameLength, p, false)) {
currentSubscription.cancel();

p.release();

final ByteBuf errorFrame =
Expand All @@ -143,6 +157,8 @@ public void onNext(@Nullable Payload p) {
return;
}
} catch (IllegalReferenceCountException e) {
currentSubscription.cancel();

final ByteBuf errorFrame =
ErrorFrameCodec.encode(
allocator,
Expand All @@ -155,16 +171,26 @@ public void onNext(@Nullable Payload p) {
try {
sendReleasingPayload(streamId, FrameType.NEXT_COMPLETE, mtu, p, sender, allocator, false);
} catch (Throwable ignored) {
currentSubscription.cancel();
}
}

@Override
public void onError(Throwable t) {
if (S.getAndSet(this, Operators.cancelledSubscription()) == Operators.cancelledSubscription()) {
if (this.done) {
logger.debug("Dropped error", t);
return;
}

final Subscription currentSubscription = this.s;
if (currentSubscription == Operators.cancelledSubscription()
|| !S.compareAndSet(this, currentSubscription, Operators.cancelledSubscription())) {
logger.debug("Dropped error", t);
return;
}

this.done = true;

final int streamId = this.streamId;

this.requesterResponderSupport.remove(streamId, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,24 @@ public void testHandleKeepAlive() throws Exception {

@Test
@Timeout(2_000)
@Disabled
public void testHandleResponseFrameNoError() throws Exception {
final int streamId = 4;
rule.connection.clearSendReceiveBuffers();

final TestPublisher<Payload> testPublisher = TestPublisher.create();
rule.setAcceptingSocket(
new RSocket() {
@Override
public Mono<Payload> requestResponse(Payload payload) {
return testPublisher.mono();
}
});
rule.sendRequest(streamId, FrameType.REQUEST_RESPONSE);

testPublisher.complete();
assertThat(
"Unexpected frame sent.",
frameType(rule.connection.awaitSend()),
anyOf(is(FrameType.COMPLETE), is(FrameType.NEXT_COMPLETE)));
testPublisher.assertWasNotCancelled();
}

@Test
Expand Down