Skip to content

tap-complete not called for last observable inside zip #7467

Open

Description

@janhommels

Describe the bug

Zip tears down all inner observables when it completes before they can run their tap implementations.
When adding a complete-function to all inner observables of a tap, the observable which will complete last and cause the whole zip to complete will not run their complete function iside their tap operator.

I understand that there are multiple solutions to this issue like using the finalize operator, however people don't expect the complete callback to not be called and it took me a while to figure that out.
I'm not sure if this issues comes up in other inner/outer-observable scenarios.

Expected behavior

The complete-callback inside the tap operator should always be called, regardless of when/how it completes.
In the example below, "2 completed" will not be logged. Expected behaviour would be that it will be logged.

Reproduction code

zip(
  of(1).pipe(tap({ next: n => console.log(`${n} nexted`), complete: _ => console.log("1 completed") })),
  of(2).pipe(tap({ next: n => console.log(`${n} nexted`), complete: _ => console.log("2 completed") }))
).subscribe({next: n => console.log(n), complete: _ => console.log("the end")})

Reproduction URL

https://playcode.io/1845913

Version

8.0.0-alpha.3

Environment

No response

Additional context

No response

Activity

  1. demensky commented on Apr 23, 2024

    @demensky
    Contributor

    It simply unsubscribes until complete happens. This can be clearly seen if more elements are passed to the second observable:

    https://playcode.io/1846169

    zip(
      of(1, 2).pipe(
        tap({
          next: n => console.log('1 nexted:', n),
          unsubscribe: () => console.log('1 unsubscribe'),
          complete: () => console.log('1 completed'),
        })
      ),
      of('a', 'b', 'c').pipe(
        tap({
          next: n => console.log('2 nexted:', n),
          unsubscribe: () => console.log('2 unsubscribe'),
          complete: () => console.log('2 completed'),
        })
      )
    ).subscribe({
      next: n => console.log('result', n),
      complete: () => console.log('the end'),
    });
  2. al-mart commented on Jul 31, 2024

    @al-mart

    I really dont think this is a bug. I see @jakovljevic-mladen tagged this as a bug but let me explain my analisis.
    the tap operators inner subscription has unsubscribe and finalize methods.
    If the subscription is terminated from the subscriber than the unsubscribe is called and then finalize.
    But if the tap operators inner subscription have already been completed the unsubscribe is not called and only finalize is called. isUnsub flag.
    The source.

    complete: () => {
                    isUnsub = false;
                    tapObserver.complete?.();
                    destination.complete();
                  },
                  finalize: () => {
                    if (isUnsub) {
                      tapObserver.unsubscribe?.();
                    }
                    tapObserver.finalize?.();
                  },
    

    Also please see this examples

    zip(
        interval(350).pipe(
            take(2),
            tap({
                next: n => console.log('A nexted:', n),
                unsubscribe: () => console.log('A unsubscribe'),
                complete: () => console.log('A completed'),
                finalize: () => console.log('A finalized'),
            })),
        interval(200).pipe(
            take(2),
            tap({
                next: n => console.log('B nexted:', n),
                unsubscribe: () => console.log('B unsubscribe'),
                complete: () => console.log('B completed'),
                finalize: () => console.log('B finalized'),
            }))
    ).subscribe({
        next: (n) => console.log('result', n),
        complete: () => console.log('the end'),
    });
    

    Result

    B nexted: 0
    A nexted: 0
    result [ 0, 0 ]
    B nexted: 1
    B completed
    B finalized
    A nexted: 1
    result [ 1, 1 ]
    the end
    A unsubscribe
    A finalized
    

    And second scenario

    zip(
        interval(350).pipe(
            take(2),
            tap({
                next: n => console.log('A nexted:', n),
                unsubscribe: () => console.log('A unsubscribe'),
                complete: () => console.log('A completed'),
                finalize: () => console.log('A finalized'),
            })),
        interval(200).pipe(
            take(2),
            tap({
                next: n => console.log('B nexted:', n),
                unsubscribe: () => console.log('B unsubscribe'),
                complete: () => console.log('B completed'),
                finalize: () => console.log('B finalized'),
            }))
    ).pipe(take(1)).subscribe({
        next: (n) => console.log('result', n),
        complete: () => console.log('the end'),
    });
    

    RESULT

    B nexted: 0
    A nexted: 0
    result [ 0, 0 ]
    the end
    A unsubscribe
    A finalized
    B unsubscribe
    B finalized
    

    The only difference here is the take operator in zip, which unsubscribes of the interval which unsubscribes from tap.
    In this case the finalize method is called before complete, As the interval is not yet emitted all the values it has for us.
    If you have any further questions I am ready to dig deeper unside this query and also I would be happy to know whether this is a correct assumption or no from Ben.

  3. rajnadan commented on Jan 4, 2026

    @rajnadan

    @janhommels
    What you’re seeing is correct RxJS behavior.

    complete() is a signal from the producer
    unsubscribe() is a decision by the consumer

    In this case, zip is the consumer.

    What zip guarantees
    Emits when:
    All sources have a value.

    Completes when:
    It has emitted all possible tuples, OR it can determine no further tuples can be formed.

    When zip knows no more tuples are possible, it may unsubscribe early from remaining sources.

    Image

    CC @demensky @al-mart

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

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions