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

chore(shareReplay): remove redundant variable isComplete #5458

Merged

Conversation

josepot
Copy link
Contributor

@josepot josepot commented May 27, 2020

Description:

This PR removes the redundant variable isComplete from the shareReplay operator. This variable became unnecessary after this change.

Copy link
Collaborator

@cartant cartant left a comment

Choose a reason for hiding this comment

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

LGTM.

@benlesh
Copy link
Member

benlesh commented May 27, 2020

Great! Thank you, @josepot!

@benlesh benlesh merged commit 9efc3d5 into ReactiveX:master May 27, 2020
@leggechr
Copy link
Contributor

leggechr commented Jul 1, 2020

This broke tests at google. I'm not entirely sure why but I don't think the behavior is exactly the same as before.

@josepot
Copy link
Contributor Author

josepot commented Jul 1, 2020

Hi @leggechr, I'm very sorry that this PR changed the behavior. I think that I know what's happening, my bad!

I'm a bit busy right now, but in a few hours I will get to this. I will add a new test for this use case and I will fix the code. Again, I'm very sorry that I broke this 🙏. I actually thought that this was the expected behavior.

@josepot
Copy link
Contributor Author

josepot commented Jul 1, 2020

I could be wrong, but I can only think of one edge-case where this change would alter the behavior, and if that's what happened, then I think that this change actually fixed a bug. I could be wrong, though 😅. Ok, so this is the only thing that I can come up with:

With the previous code: If the refCount drops to zero and the source hasn't completed, then the first thing that happens is the unsubscription from the source. If that tear-down logic causes for the source to complete while it's being unsubscribed from... 🤯 then that would set the flag isComplete to true... So, the next time that a new subscriber comes, then there is a new subscription to the source, however the flag isComplete is still set to true... But that wouldn't be accurate, right? So, in this hypothetical case, if the refCount drops to zero again but the new subscription against source hasn't completed, since the flag is saying that it did complete, then it won't unsubscribe from the source.

This is the only change of behavior that I can come up with, and if that's the case, then I think that the new code is fixing a bug?

I'm sure that I must be missing something, though.

@josepot
Copy link
Contributor Author

josepot commented Jul 1, 2020

Ups! @leggechr I found the issue!

Consider the following code:

        subscription = source.subscribe({
          next(value: T) {
            subject!.next(value);
          },
          error(err: any) {
            hasError = true;
            subject!.error(err);
          },
          complete() {
            subscription = undefined;
            subject!.complete();
          }
        });
      }

if the source completes synchronously upon subscription, for instance:

from([1, 2, 3, 4, 5]).pipe(
  shareReplay({ refCount: true, bufferSize: 1 })
)

then the subscription is set first to undefined and then it's set to the "closed subscription" 🤦 That's it! That's why the variable isComplete is needed. Nah, I don't want to bring that redundant variable back. It's too confusing. I would do this instead:

if (subscription && !subscription.closed && useRefCount && refCount === 0) {

I'm afraid that the reason why the tests are not catching this issue is because of #5523 😕

cartant added a commit to cartant/rxjs that referenced this pull request Jul 1, 2020
@cartant cartant mentioned this pull request Jul 1, 2020
@josepot josepot deleted the chore/groupby-remove-redundant-variable branch July 1, 2020 23:04
benlesh pushed a commit that referenced this pull request Jul 2, 2020
* test: add failing test

* Revert "chore(shareReplay): remove redundant variable `isComplete` (#5458)"

This reverts commit 9efc3d5.
@leggechr
Copy link
Contributor

leggechr commented Jul 2, 2020

No worries @josepot! We sync rx at head into Google to test for things like this to catch them before going out to the general public. So I'm glad we could catch it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants