Skip to content

RSocketResponder does not remove Subscriber from sendingSubscriptions when response is empty #961

Closed

Description

@hadix-lin

io.rsocket.core.RSocketResponder#handleRequestResponse

private void handleRequestResponse(int streamId, Mono<Payload> response) {
    final BaseSubscriber<Payload> subscriber =
        new BaseSubscriber<Payload>() {
          private boolean isEmpty = true;

          @Override
          protected void hookOnNext(Payload payload) {
            if (isEmpty) {
              isEmpty = false;
            }

            if (!PayloadValidationUtils.isValid(mtu, payload, maxFrameLength)) {
              payload.release();
              cancel();
              final IllegalArgumentException t =
                  new IllegalArgumentException(INVALID_PAYLOAD_ERROR_MESSAGE);
              handleError(streamId, t);
              return;
            }

            ByteBuf byteBuf =
                PayloadFrameCodec.encodeNextCompleteReleasingPayload(allocator, streamId, payload);
            sendProcessor.onNext(byteBuf);
          }

          @Override
          protected void hookOnError(Throwable throwable) {
            if (sendingSubscriptions.remove(streamId, this)) {
              handleError(streamId, throwable);
            }
          }

          @Override
          protected void hookOnComplete() {
            if (isEmpty) {
              if (sendingSubscriptions.remove(streamId, this)) {
                sendProcessor.onNext(PayloadFrameCodec.encodeComplete(allocator, streamId));
              }
            }
          }
        };

    sendingSubscriptions.put(streamId, subscriber); 
    response.doOnDiscard(ReferenceCounted.class, DROPPED_ELEMENTS_CONSUMER).subscribe(subscriber);
  }

set isEmpty=false in hookOnNext, but there is no code to set it to true afterwards. sendingSubscriptions.remove(streamId, this) in hookOnComplete has no chance to be executed. cause the subscriber object cannot be recycle.

Why not put sendingSubscriptions.remove(streamId, this) into hookFinally?

Activity

  1. linux-china commented on Nov 19, 2020

    @linux-china
    Contributor

    Then link is:

    private void handleRequestResponse(int streamId, Mono<Payload> response) {

    or remove directly in hookOnComplete like logic for requestStream

     protected void hookOnComplete() {
                if (sendingSubscriptions.remove(streamId, this)) {
                  sendProcessor.onNext(PayloadFrameCodec.encodeComplete(allocator, streamId));
                }
    }
  2. rstoyanchev commented on Nov 19, 2020

    @rstoyanchev
    Contributor

    It does look like sendingSubscriptions.remove should be called in hookOnComplete in any case. Perhaps the condition should be (isEmpty && sendingSubscriptions.remove(streamId, this)). WDYT @OlegDokuka ?

  3. OlegDokuka commented on Nov 19, 2020

    @OlegDokuka
    Member

    Looks like a bug. We have to call remove inside hookOnNext for sure.

    IsEmpty is an important flag and has to be there in order to prevent Next Complete frame followed by Complete frame

  4. added this to the 1.0.4 milestone on Nov 19, 2020
  5. changed the title [-]There may be a memory leak issue in version 1.0.2[/-] [+]RSocketResponder does not remove Subscriber from sendingSubscription when response is empty[/+] on Nov 19, 2020
  6. changed the title [-]RSocketResponder does not remove Subscriber from sendingSubscription when response is empty[/-] [+]RSocketResponder does not remove Subscriber from sendingSubscriptions when response is empty[/+] on Nov 19, 2020
  7. added 2 commits that reference this issue on Nov 20, 2020
    a7000fa
    ae94de0
  8. removed this from the 1.0.4 milestone on Nov 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsupersededIssue is superseded by another

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions