[MessageBar] Fix NullReferenceException and thread safety in MessageService#4677
Closed
JamesNK wants to merge 3 commits intomicrosoft:mainfrom
Closed
[MessageBar] Fix NullReferenceException and thread safety in MessageService#4677JamesNK wants to merge 3 commits intomicrosoft:mainfrom
JamesNK wants to merge 3 commits intomicrosoft:mainfrom
Conversation
- 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
vnbaaij
previously approved these changes
Apr 7, 2026
Collaborator
|
Hi @JamesNK please re-target this PR to go to the dev branch |
dvoituron
approved these changes
Apr 7, 2026
dvoituron
requested changes
Apr 7, 2026
Collaborator
dvoituron
left a comment
There was a problem hiding this comment.
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
Collaborator
|
Rolled up into #4680. Thanks again for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix #4674
NullReferenceException in
ShowMessageBarAsyncShowMessageBarAsynccalledawait OnMessageItemsUpdatedAsync!.Invoke()without a null check. IfShowMessageBarAsyncis called beforeFluentMessageBarProvider.OnInitializedsubscribes to the event, this throws aNullReferenceException. The delegate is now captured in a local variable before invoking for thread safety:Thread safety of
AllMessagesandMessagesToShowBoth properties/methods acquired a read lock but returned the live
MessageListreference. The lock was released before the caller could enumerate, so concurrent writes (e.g. adding a message) could modify the collection during enumeration, causingInvalidOperationException.Both now return a snapshot copy via
.ToList()while still holding the read lock.