Skip to content

MudDialog: Fix exception when closing a dialog while rendering#10226

Merged
ScarletKuro merged 6 commits intoMudBlazor:devfrom
vernou:10180-dialog-async
Nov 23, 2024
Merged

MudDialog: Fix exception when closing a dialog while rendering#10226
ScarletKuro merged 6 commits intoMudBlazor:devfrom
vernou:10180-dialog-async

Conversation

@vernou
Copy link
Member

@vernou vernou commented Nov 11, 2024

Description

Fixes #10180
Fixes #8497

Closing a dialog while rendering throws the exception:

System.InvalidOperationException : Collection was modified; enumeration operation may not execute.
   at System.Collections.Generic.List`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at MudBlazor.MudDialogProvider.OnAfterRenderAsync(Boolean firstRender) in C:\repos\vernou\MudBlazor\src\MudBlazor\Components\Dialog\MudDialogProvider.razor.cs:line 137
   at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleAfterRender.OnAfterRenderAsync()
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.NotifyRenderCompletedAsync()

The reason is that rendering enumerate MudDialogProvider._dialogs and closing remove a element in MudDialogProvider._dialogs.

The solution is to use a concurrent collection.

How Has This Been Tested?

I added a test to reproduce the bug.

Type of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (fix or improvement to the website or code docs)

Checklist

  • The PR is submitted to the correct branch (dev).
  • My code follows the code style of this project.
  • I've added relevant tests.

@github-actions github-actions bot added the bug Unexpected behavior or functionality not working as intended label Nov 11, 2024
@vernou vernou self-assigned this Nov 11, 2024
@codecov
Copy link

codecov bot commented Nov 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.55%. Comparing base (0529c1a) to head (fe116c0).
Report is 1 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #10226      +/-   ##
==========================================
+ Coverage   91.53%   91.55%   +0.01%     
==========================================
  Files         414      414              
  Lines       13014    13015       +1     
  Branches     2454     2455       +1     
==========================================
+ Hits        11913    11916       +3     
+ Misses        551      549       -2     
  Partials      550      550              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@vernou vernou marked this pull request as ready for review November 12, 2024 00:03
Copy link
Member

@ScarletKuro ScarletKuro left a comment

Choose a reason for hiding this comment

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

LGTM in general

@ScarletKuro ScarletKuro requested a review from henon November 12, 2024 10:14
Copy link
Contributor

@henon henon left a comment

Choose a reason for hiding this comment

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

Good job! Just fix the exception message, then it can be merged.

@henon henon changed the title MudDialog : Fix closing a dialog while rendering MudDialog : Fix exception when closing a dialog while rendering Nov 12, 2024
@henon henon changed the title MudDialog : Fix exception when closing a dialog while rendering MudDialog: Fix exception when closing a dialog while rendering Nov 12, 2024
@vernou
Copy link
Member Author

vernou commented Nov 12, 2024

@henon, I fixed the exception message.

@ScarletKuro
Copy link
Member

Very interesting but NestedInlineDialogTest test is failing? I tried locally and it's constantly failing. When i revert locally 341787b then it passes? What's happening and how exception message affects the test I don't understand that.

@ScarletKuro
Copy link
Member

Very interesting but NestedInlineDialogTest test is failing? I tried locally and it's constantly failing. When i revert locally 341787b then it passes? What's happening and how exception message affects the test I don't understand that.

It was just coincidence, doesn't matter with or without 341787b, the test is just flaky as hell now

@ScarletKuro
Copy link
Member

ScarletKuro commented Nov 12, 2024

It was just coincidence, doesn't matter with or without 341787b, the test is just flaky as hell now

It's not that test flaky, if you play with TestNestedInlineDialog in the viewer, and open & close the nested dialog back and forth, as some moment you can get a big amount of nested dialog open at same time.

@vernou
Copy link
Member Author

vernou commented Nov 12, 2024

I fail to reproduce.
@ScarletKuro , can you share a video?

@ScarletKuro
Copy link
Member

ScarletKuro commented Nov 12, 2024

@ScarletKuro , can you share a video?

with your changes against latest dev
also notice the delay it takes to open the dialog
broken
without your change against latest dev
shouldbe

@ScarletKuro
Copy link
Member

Btw, if you struggle to fix it using ConcurrentDictionary, the easiest fix would be to copy the list on iteration. Yes it's expensive, but it does fix such scenarios when list modifies in the middle of iteration,

@ScarletKuro ScarletKuro self-requested a review November 15, 2024 20:38
@vernou
Copy link
Member Author

vernou commented Nov 16, 2024

I think the problem is that MudDialog use OnAfterRenderAsync to show/hide nested inline dialog... but it will be a lot of work to rework it. So I applied your sugestion that work.

As I previously used ConcurrentDictionnary.Value that already did a copy, so it will not degrade performance.

@vernou vernou force-pushed the 10180-dialog-async branch from 4267d75 to f061fc3 Compare November 16, 2024 23:58
@vernou
Copy link
Member Author

vernou commented Nov 17, 2024

@ScarletKuro , the PR sound ready to the next review.

@vernou
Copy link
Member Author

vernou commented Nov 19, 2024

@ScarletKuro ping

@ScarletKuro
Copy link
Member

@ScarletKuro ping

Will double check it on friday/weekends.

@ScarletKuro ScarletKuro assigned ScarletKuro and unassigned vernou Nov 19, 2024
@danielchalmers danielchalmers removed their request for review November 22, 2024 16:37
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
B Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@ScarletKuro ScarletKuro merged commit a4be429 into MudBlazor:dev Nov 23, 2024
@vernou vernou deleted the 10180-dialog-async branch November 23, 2024 17:15
@vernou vernou self-assigned this Nov 23, 2024
LLauter pushed a commit to cannellamedia/MudBlazor that referenced this pull request Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected behavior or functionality not working as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Closing a MudDialog too early can cause crash DialogService exception when awaiting Task.CompletedTask between ShowAsync and Close.

3 participants