Skip to content

MudFocusTrap: guard against disposal race in OnAfterRenderAsync#12252

Merged
danielchalmers merged 1 commit intoMudBlazor:devfrom
jpacc260:fix/mudfocustrap-js-error
Dec 17, 2025
Merged

MudFocusTrap: guard against disposal race in OnAfterRenderAsync#12252
danielchalmers merged 1 commit intoMudBlazor:devfrom
jpacc260:fix/mudfocustrap-js-error

Conversation

@jpacc260
Copy link
Contributor

Fixes #11862

Problem

MudFocusTrap.OnAfterRenderAsync can call InitializeFocusAsync() after the component has been disposed which causes an exception. This can happen because await SaveFocusAsync() yields; during that await, _disposed may flip to true, but the method then continues and reaches initialization.

Fix

Add a second _disposed guard after await SaveFocusAsync() and before the initialization path. This prevents focus initialization from running after disposal.

Repro (reliable)

Insert a temporary delay after SaveFocusAsync() (e.g. await Task.Delay(3000)), open a dialog and immediately close it (“OK”), then wait—without the extra _disposed check, InitializeFocusAsync() can run post-dispose and crash.

Notes

This change only adds a defensive disposal guard; behaviour is unchanged otherwise.

Additional context and analysis

@ScarletKuro ScarletKuro requested a review from versile2 December 12, 2025 12:55
@danielchalmers danielchalmers added the bug Unexpected behavior or functionality not working as intended label Dec 14, 2025
@danielchalmers danielchalmers merged commit 2954c90 into MudBlazor:dev Dec 17, 2025
7 checks passed
@danielchalmers
Copy link
Member

Thank you!

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.

MudFocusTrap JS ERROR

4 participants