Skip to content

bug(MatDialog): When opening multiple dialogs in a short amount of time, some of them do not open #24037

Closed

Description

@guimabdo

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

12.2.0

Description

When opening multiple dialogs in a short amount of time, some of them do not open

Reproduction

The bug can be noticed with the example below, when clicking the "Open Fast" button.

import { Component, TemplateRef, ViewChild } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';

@Component({
  selector: 'app-root',
  template: `
  <button (click)="openFast()">Open Fast</button>
  <button (click)="openSlow()">Open Slow</button>
  <ng-template let-data #template>
    {{data.message}}
    <button mat-dialog-close>close</button>
  </ng-template>
  `
})
export class AppComponent {
  @ViewChild("template") template?: TemplateRef<any>;
  constructor(private matDialog: MatDialog){}
  openFast(){
    this.open('1');
    setTimeout(() => this.open('2'), 1);
    setTimeout(() => this.open('3'), 50);
    setTimeout(() => this.open('4'), 100);
    setTimeout(() => this.open('5'), 150);
    setTimeout(() => this.open('6'), 200);
  }
  openSlow(){
    this.open('1');
    setTimeout(() => this.open('2'), 300);
    setTimeout(() => this.open('3'), 600);
    setTimeout(() => this.open('4'), 900);
    setTimeout(() => this.open('5'), 1200);
    setTimeout(() => this.open('6'), 1500);
  }

  private open(message: string){
    this.matDialog.open(this.template!, { 
      disableClose: true,
      data: { message } 
    });
  }
}

Expected Behavior

"Open fast" button should open 6 dialogs, just like the "Open slow" button.

Actual Behavior

The "Open fast" button opens only 2 dialogs.
On most attempts it opens (1) and (5), but in some cases (1) and (6) are open.

Environment

  • Angular: 13.0.0
  • CDK/Material: 13.0.0
  • Browser(s): Chrome
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows

Activity

  1. added
    needs triage
    on Nov 30, 2021
  2. hijamoya commented on Dec 1, 2021

    @hijamoya

    We encounter the same issue, If we call open in ngOnInit like below:

      ngOnInit() {
          setTimeout(() => this.open('2'), 0);
      }
    

    The dialog dose not open anymore, it is fine in previous version.

    However, if we just:

      ngOnInit() {
          this.open('2');
      }
    

    The dialog will be opened correctly.

  3. lephyrus commented on Dec 2, 2021

    @lephyrus

    We've also hit this issue. I'm sure it was introduced with this commit. There's a comment on it that points to the underlying issue.

    The commit message says the goal is to [a]void opening multiple of the same dialog before animations complete by returning the previous MatDialogRef, but the change does not check whether the two dialogs are the same. This leads to really weird behaviour, which took us a long time to figure out. Here's our use case (which was working fine before):

    // injected in real code
    declare const dialog: MatDialog;
    
    // somewhere in the codebase
    const dialogA = dialog.open(DialogA); // dialog that shows a spinner while async thing A is happening
    
    // somewhere else in codebase, gets executed as part of A succeeding but can fail independently
    const dialogB = dialog.open(DialogB); // dialog that shows an error
    dialogB.afterClosed().pipe(/* do stuff according to user response */);

    The way the timing works out in our case, dialogB will sometimes be a reference to DialogB (MatDialogRef<DialogB>, as guaranteed by the typing of open()), but mostly it will be a reference to DialogA and (more crucially) dialog B wasn't opened and we're processing a response that was given for the other dialog.

    Any chance you could take another look at this, @Splaktar?

  4. Splaktar commented on Dec 2, 2021

    @Splaktar
    Contributor

    Thank you for the example code. It would be great if you could provide a runnable StackBlitz to help us investigate this more efficiently.

  5. added
    needs: clarification
    on Dec 2, 2021
  6. Splaktar commented on Dec 2, 2021

    @Splaktar
    Contributor

    Does this still happen when you import NoopAnimations instead of BrowserAnimations?

    @Inject(ANIMATION_MODULE_TYPE)
    animationMode?: 'NoopAnimations' | 'BrowserAnimations',

  7. Splaktar commented on Dec 2, 2021

    @Splaktar
    Contributor

    @lephyrus there is a lot of discussion about the tradeoffs and issues that we faced here in the following PRs

  8. Splaktar commented on Dec 2, 2021

    @Splaktar
    Contributor

    If I had a StackBlitz for this, I could try out a few quick things right now and see if I can provide a workaround, but since I don't, I probably won't have time to look into this again this week.

  9. added
    Accessibility
    needs investigation
    and removed
    needs triage
    on Dec 2, 2021
  10. 30 remaining items

  11. rasoulMrz commented on Feb 26, 2022

    @rasoulMrz

    @aikrez okay, I do have an update. There have been some undesirable side effects of this change in the presubmit tests. They are being investigated and potential solutions and workarounds are being evaluated.

    Any updates?

  12. P-de-Jong commented on Mar 2, 2022

    @P-de-Jong

    Would like to know too when this PR is getting merged. We're having a lot of problems with this issue at the moment 😢

  13. proggler23 commented on Mar 3, 2022

    @proggler23

    It seems like opening a dialog from within another dialog also fails if there happened to be an error shortly before (within the animation duration). I cannot listen on "dialogOpened" observable, as this only triggers once, but the error will trigger the dialogContainer.start animation again.

  14. crisbeto commented on Mar 5, 2022

    @crisbeto
    Member

    I reworked the fix for this on Friday to make it easier to merge and got it passing all the internal checks. It'll be merged in on Monday and should be out in the next patch release.

  15. DoodahProductions commented on Mar 8, 2022

    @DoodahProductions

    Hello, thanks for this fix !
    I'm not sure what npm package I should update to fetch this fix, may you help me ? Thanks :)

  16. crisbeto commented on Mar 9, 2022

    @crisbeto
    Member

    The fix will be in @angular/material, but we haven't made a release with the new code yet. It should be out later today/tomorrow.

  17. Splaktar commented on Mar 10, 2022

    @Splaktar
    Contributor

    This fix is included in 13.2.6 and 14.0.0-next.6.

  18. P-de-Jong commented on Mar 11, 2022

    @P-de-Jong

    We have updated our applications to the new patch and everything seems to work as intended. Thanks!

  19. angular-automatic-lock-bot commented on Apr 11, 2022

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  20. locked and limited conversation to collaborators on Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

AccessibilityP2area: material/dialog

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions