-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix IIS/Windows Service console race condition (#7691) #7793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Aaronontheweb
merged 2 commits into
akkadotnet:v1.5
from
Aaronontheweb:fix/7691-iis-console-race-condition
Sep 4, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| //----------------------------------------------------------------------- | ||
| // <copyright file="StandardOutWriterSpec.cs" company="Akka.NET Project"> | ||
| // Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com> | ||
| // Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
| // </copyright> | ||
| //----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| using Akka.TestKit; | ||
| using Akka.Util; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Akka.Tests.Loggers | ||
| { | ||
| /// <summary> | ||
| /// Tests for StandardOutWriter to ensure it handles IIS/Windows Service environments correctly | ||
| /// where Console.Out and Console.Error may be redirected to StreamWriter.Null | ||
| /// </summary> | ||
| public class StandardOutWriterSpec : AkkaSpec | ||
| { | ||
| public StandardOutWriterSpec(ITestOutputHelper output) : base(output) | ||
| { | ||
| } | ||
|
|
||
| [Fact] | ||
| public void StandardOutWriter_should_handle_concurrent_writes_without_race_conditions() | ||
| { | ||
| // This test simulates the concurrent access pattern that causes issues in IIS | ||
| // In normal test environments this won't reproduce the issue, but it ensures | ||
| // our fix doesn't break normal console operation | ||
|
|
||
| var tasks = new Task[100]; | ||
|
|
||
| for (int i = 0; i < tasks.Length; i++) | ||
| { | ||
| var taskId = i; | ||
| tasks[i] = Task.Run(() => | ||
| { | ||
| for (int j = 0; j < 10; j++) | ||
| { | ||
| // These calls should not throw even under concurrent access | ||
| StandardOutWriter.WriteLine($"Task {taskId} - Line {j}"); | ||
| StandardOutWriter.Write($"Task {taskId} - Write {j} "); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Should complete without throwing IndexOutOfRangeException | ||
| Assert.True(Task.WaitAll(tasks, TimeSpan.FromSeconds(5))); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void StandardOutWriter_should_not_throw_when_console_is_redirected() | ||
| { | ||
| // Save original streams | ||
| var originalOut = Console.Out; | ||
| var originalError = Console.Error; | ||
|
|
||
| try | ||
| { | ||
| // Simulate IIS/Windows Service environment by redirecting to null | ||
| Console.SetOut(StreamWriter.Null); | ||
| Console.SetError(StreamWriter.Null); | ||
|
|
||
| // These should not throw even when console is redirected to null | ||
| StandardOutWriter.WriteLine("This should not throw"); | ||
| StandardOutWriter.Write("Neither should this"); | ||
|
|
||
| // Test with colors (which would normally fail in IIS) | ||
| StandardOutWriter.WriteLine("Colored output", ConsoleColor.Red); | ||
| StandardOutWriter.Write("Colored write", ConsoleColor.Blue, ConsoleColor.Yellow); | ||
| } | ||
| finally | ||
| { | ||
| // Restore original streams | ||
| Console.SetOut(originalOut); | ||
| Console.SetError(originalError); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void StandardOutWriter_should_handle_null_and_empty_messages() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably the most useful test of the bunch tbh |
||
| { | ||
| // Should not throw | ||
| StandardOutWriter.WriteLine(null); | ||
| StandardOutWriter.WriteLine(""); | ||
| StandardOutWriter.Write(null); | ||
| StandardOutWriter.Write(""); | ||
| } | ||
| } | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| //----------------------------------------------------------------------- | ||
|
|
||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace Akka.Util | ||
| { | ||
|
|
@@ -16,6 +17,34 @@ namespace Akka.Util | |
| public static class StandardOutWriter | ||
| { | ||
| private static readonly object _lock = new(); | ||
| private static readonly bool _isConsoleAvailable = DetectConsoleAvailability(); | ||
|
|
||
| /// <summary> | ||
| /// Detects whether a real console is available for output. | ||
| /// In environments like IIS and Windows Services, console output is redirected to StreamWriter.Null, | ||
| /// which is a singleton. When multiple threads write to both Console.Out and Console.Error | ||
| /// (which point to the same StreamWriter.Null instance), it causes race conditions. | ||
| /// | ||
| /// Since console output goes nowhere in these environments anyway, we skip it entirely | ||
| /// to prevent the race condition and improve performance. | ||
| /// </summary> | ||
| private static bool DetectConsoleAvailability() | ||
| { | ||
| // Specifically detect the IIS/Windows Service scenario where both Console.Out | ||
| // and Console.Error point to the SAME StreamWriter.Null singleton instance. | ||
| // This is the exact condition that causes the race condition. | ||
| // Note: We check both because in these environments, both are always set to the same instance | ||
| if (Console.Out == StreamWriter.Null && Console.Error == StreamWriter.Null) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment in the code explains it best |
||
| return false; | ||
|
|
||
| // Also check Environment.UserInteractive for additional safety | ||
| // This returns false for Windows Services and IIS in .NET Framework | ||
| // (though less reliable in .NET Core, the StreamWriter.Null check above is the key) | ||
| if (!Environment.UserInteractive) | ||
| return false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes the specified <see cref="string"/> value to the standard output stream. Optionally | ||
|
|
@@ -46,6 +75,16 @@ public static void WriteLine(string message, ConsoleColor? foregroundColor = nul | |
| private static void WriteToConsole(string message, ConsoleColor? foregroundColor = null, | ||
| ConsoleColor? backgroundColor = null, bool line = true) | ||
| { | ||
| // Skip console output in IIS, Windows Services, and other non-console environments. | ||
| // In these environments: | ||
| // 1. Console output is redirected to StreamWriter.Null (goes nowhere anyway) | ||
| // 2. Both Console.Out and Console.Error point to the same StreamWriter.Null singleton | ||
| // 3. Concurrent writes to both streams cause race conditions and IndexOutOfRangeException | ||
| // 4. Skipping output entirely prevents the race condition and improves performance | ||
| // See: https://github.com/akkadotnet/akka.net/issues/7691 | ||
| if (!_isConsoleAvailable) | ||
| return; | ||
|
|
||
| lock (_lock) | ||
| { | ||
| ConsoleColor? fg = null; | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole test class is probably a tad useless IMHO given the racy and rare nature of this bug but it's worth a shot