Skip to content

Fix xUnit headless tests losing TestContext on background thread - #21357

Merged
MrJul merged 3 commits into
AvaloniaUI:masterfrom
mikegoatly:fix/xunit-headless-execution-context
May 15, 2026
Merged

Fix xUnit headless tests losing TestContext on background thread#21357
MrJul merged 3 commits into
AvaloniaUI:masterfrom
mikegoatly:fix/xunit-headless-execution-context

Conversation

@mikegoatly

Copy link
Copy Markdown
Contributor

What does the pull request do?

Attempts to mitigate the intermittant xUnit exception seen when running Headless tests. See #21332 for discussions.

What is the current behavior?

Running headless tests in a single test assembly periodically results in an exception:

System.InvalidOperationException
  HResult=0x80131509
  Message=Cannot get KeyValueStorage on the idle test context
  Source=xunit.v3.core
  StackTrace:
   at Xunit.TestContext.get_KeyValueStorage() in /_/src/xunit.v3.core/TestContext.cs:line 111
   at Xunit.TestContext.SetForTest(ITest test, TestEngineStatus testStatus, CancellationToken cancellationToken, TestResultState testState, ITestOutputHelper testOutputHelper, Object testClassInstance) in /_/src/xunit.v3.core/TestContext.cs:line 360

What is the updated/expected behavior with this PR?

The exception doesn't happen.

Breaking changes

None I'm aware of.

Fixed issues

Fixes #21332

@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.1.999-cibuild0065349-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul added bug area-headless backport-candidate-12.0.x Consider this PR for backporting to 12.0 branch labels May 13, 2026
Comment thread src/Headless/Avalonia.Headless/HeadlessUnitTestSession.cs
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.1.999-cibuild0065365-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@mikegoatly
mikegoatly force-pushed the fix/xunit-headless-execution-context branch from 0d54f72 to 33f4c22 Compare May 14, 2026 17:17
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.1.999-cibuild0065401-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@MrJul
MrJul enabled auto-merge May 15, 2026 09:07
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.1.999-cibuild0065409-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul
MrJul added this pull request to the merge queue May 15, 2026
Merged via the queue into AvaloniaUI:master with commit 85263da May 15, 2026
11 checks passed
MrJul added a commit to MrJul/Avalonia that referenced this pull request May 28, 2026
…loniaUI#21357)

* Fix xUnit headless tests losing TestContext on background thread

* Capture context on sync invocations as well

---------

Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
@MrJul MrJul added backported-12.0.x and removed backport-candidate-12.0.x Consider this PR for backporting to 12.0 branch labels May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v12] Headless Test Case Cleanup Failure with 'Cannot get KeyValueStorage on the idle test context'

4 participants