Skip to content

Commit 50d6d0b

Browse files
Copilotstephentoub
andauthored
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>
1 parent 5084a79 commit 50d6d0b

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

tests/Common/Utils/XunitLoggerProvider.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,20 @@ public void Log<TState>(
3434
sb.Append(exception.ToString());
3535
}
3636

37-
output.WriteLine(sb.ToString());
37+
try
38+
{
39+
output.WriteLine(sb.ToString());
40+
}
41+
catch (InvalidOperationException)
42+
{
43+
// Ignore exceptions from xUnit's TestOutputHelper when the test has already completed.
44+
// Background work may continue logging after xUnit has disposed the test context.
45+
}
46+
catch (NullReferenceException)
47+
{
48+
// xUnit v3 may throw NullReferenceException in TestOutputHelper.QueueTestOutput()
49+
// when the internal queue has been torn down after test completion.
50+
}
3851
}
3952

4053
public bool IsEnabled(LogLevel logLevel) => true;

0 commit comments

Comments
 (0)