Skip to content

fix: prevent null WrappedElement during concurrent component re-renders - #1946

Open
Nacl-HCl wants to merge 1 commit into
bUnit-dev:mainfrom
Nacl-HCl:fix/wrapper-base-element-null-race-1937
Open

Nacl-HCl wants to merge 1 commit into
bUnit-dev:mainfrom
Nacl-HCl:fix/wrapper-base-element-null-race-1937

Conversation

@Nacl-HCl

@Nacl-HCl Nacl-HCl commented Oct 18, 2026 •

Copy link
Copy Markdown

Pull request description

Fixes a race condition in WrapperBase<TElement>.WrappedElement where an element returned by Find() could return null and throw a NullReferenceException on member access when the component re-renders on another thread at the same time (e.g. from a background timer or asynchronous event).

Cause

The getter previously read the element field twice:
`csharp
if (element is null)
element = elementFactory.GetElement();

return element;
WhenOnElementReplacedresetelementtonullbetween the check and the return statement, the getter returnednull`.

Fix

Changed the getter to evaluate via null-coalescing assignment element ??= elementFactory.GetElement<TElement>(), which ensures the reference is evaluated once and never returns null. Also updated the verified generator snapshot and added a concurrent regression test.

Closes #1937

PR meta checklist

  • Pull request is targeted at main branch for code
  • Pull request is linked to all related issues, if any.
  • I have read the CONTRIBUTING.md document.

Code PR specific checklist

  • My code follows the code style of this project and AspNetCore coding guidelines.
  • My change requires a change to the documentation.
  • I have updated the appropriate sub section in the CHANGELOG.md.
  • I have added, updated or removed tests to according to my changes.
    • All tests passed.

Use null-coalescing assignment in WrapperBase.WrappedElement getter to ensure the field is only read once during retrieval, avoiding race conditions where OnElementReplaced clears the field before the return.

Closes bUnit-dev#1937
@linkdotnet

Copy link
Copy Markdown
Collaborator

Looking good - let's wait what CI/CD is saying and then get that in.
Thanks for that

Comment thread CHANGELOG.md

### Fixed

- `WrapperBase<TElement>.WrappedElement` could return `null` and throw `NullReferenceException` when an element was accessed concurrently with component re-renders. Reported by [@psu-prog](https://github.com/psu-prog) in #1937. Fixed by [@Nacl-HCl](https://github.com/Nacl-HCl).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Using < and > leads to issues (see other generic usages) as we "merge" that inside the csproj - so it leads to malformed csproj file. Either drop the < > or use &lt; stuff

This branch has not been deployed

No deployments
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.

Find() element can be null while the component re-renders on another thread (NullReferenceException in GetBunitContext)

2 participants