From 50d6d0b5709ae8631672d7dcea20c3ad6a1a231d Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Fri, 30 Jan 2026 14:56:12 -0800 Subject: [PATCH] Fix XunitLoggerProvider crash when background work logs after test disposal (#1229) Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --- tests/Common/Utils/XunitLoggerProvider.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/Common/Utils/XunitLoggerProvider.cs b/tests/Common/Utils/XunitLoggerProvider.cs index 9e77c1f..2fb76fc 100644 --- a/tests/Common/Utils/XunitLoggerProvider.cs +++ b/tests/Common/Utils/XunitLoggerProvider.cs @@ -34,7 +34,20 @@ public class XunitLoggerProvider(ITestOutputHelper output) : ILoggerProvider sb.Append(exception.ToString()); } - output.WriteLine(sb.ToString()); + try + { + output.WriteLine(sb.ToString()); + } + catch (InvalidOperationException) + { + // Ignore exceptions from xUnit's TestOutputHelper when the test has already completed. + // Background work may continue logging after xUnit has disposed the test context. + } + catch (NullReferenceException) + { + // xUnit v3 may throw NullReferenceException in TestOutputHelper.QueueTestOutput() + // when the internal queue has been torn down after test completion. + } } public bool IsEnabled(LogLevel logLevel) => true;