Skip to content

[MessageBar] Fix NullReferenceException and thread safety in MessageService#4677

Closed
JamesNK wants to merge 3 commits intomicrosoft:mainfrom
JamesNK:fix/message-service-thread-safety
Closed

[MessageBar] Fix NullReferenceException and thread safety in MessageService#4677
JamesNK wants to merge 3 commits intomicrosoft:mainfrom
JamesNK:fix/message-service-thread-safety

Conversation

@JamesNK
Copy link
Copy Markdown
Member

@JamesNK JamesNK commented Apr 7, 2026

Summary

Fix #4674

NullReferenceException in ShowMessageBarAsync

ShowMessageBarAsync called await OnMessageItemsUpdatedAsync!.Invoke() without a null check. If ShowMessageBarAsync is called before FluentMessageBarProvider.OnInitialized subscribes to the event, this throws a NullReferenceException. The delegate is now captured in a local variable before invoking for thread safety:

if (OnMessageItemsUpdatedAsync is { } handler)
{
    await handler.Invoke();
}

Thread safety of AllMessages and MessagesToShow

Both properties/methods acquired a read lock but returned the live MessageList reference. The lock was released before the caller could enumerate, so concurrent writes (e.g. adding a message) could modify the collection during enumeration, causing InvalidOperationException.

Both now return a snapshot copy via .ToList() while still holding the read lock.

- Add null check on OnMessageItemsUpdatedAsync before invoking in
  ShowMessageBarAsync, using local variable capture for thread safety.
- Return snapshot copies from AllMessages and MessagesToShow instead of
  the live MessageList reference to prevent concurrent modification errors.

Fixes microsoft#4674
@MarvinKlein1508 MarvinKlein1508 added this to the v4.14.1 milestone Apr 7, 2026
vnbaaij
vnbaaij previously approved these changes Apr 7, 2026
@vnbaaij vnbaaij changed the title fix: NullReferenceException and thread safety in MessageService [MessageBar] Fix NullReferenceException and thread safety in MessageService Apr 7, 2026
@vnbaaij vnbaaij enabled auto-merge (squash) April 7, 2026 09:07
@vnbaaij vnbaaij linked an issue Apr 7, 2026 that may be closed by this pull request
@vnbaaij vnbaaij disabled auto-merge April 7, 2026 09:12
@vnbaaij vnbaaij dismissed their stale review April 7, 2026 09:12

Not tergeting correct branch

@vnbaaij
Copy link
Copy Markdown
Collaborator

vnbaaij commented Apr 7, 2026

Hi @JamesNK please re-target this PR to go to the dev branch

Copy link
Copy Markdown
Collaborator

@dvoituron dvoituron left a comment

Choose a reason for hiding this comment

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

As Vincent pointed out, the PR should be merged into dev, not main

vnbaaij added a commit that referenced this pull request Apr 7, 2026
@vnbaaij
Copy link
Copy Markdown
Collaborator

vnbaaij commented Apr 7, 2026

Rolled up into #4680. Thanks again for your contribution!

@vnbaaij vnbaaij closed this Apr 7, 2026
vnbaaij added a commit that referenced this pull request Apr 7, 2026
* Implement #4677 (Fix #4674) in correct branch

* Implement #4678 (Fix #4676) in correct branch
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.

fix: NullReferenceException in MessageService.ShowMessageBarAsync

4 participants