Fix IIS/Windows Service console race condition (#7691)#7793
Merged
Aaronontheweb merged 2 commits intoSep 4, 2025
Merged
Conversation
- Detect when running in IIS/Windows Service environments where Console.Out and Console.Error are redirected to the same StreamWriter.Null singleton - Skip console output entirely in these environments to prevent race conditions that cause IndexOutOfRangeException and cascade failures - Improve DefaultLogger error handling to prevent feedback loops - Add unit tests for non-console scenarios The race condition occurs because: 1. IIS/Services redirect both Console.Out and Console.Error to StreamWriter.Null 2. StreamWriter.Null is a singleton, not thread-safe for concurrent access 3. Multiple threads writing to both streams cause IndexOutOfRangeException 4. Console output goes nowhere in these environments anyway Fixes akkadotnet#7691
- Make console detection more precise: only skip output when both Console.Out AND Console.Error point to StreamWriter.Null (the exact race condition scenario) - Remove unnecessary try-catch in DefaultLogger.Print() since Tell() is unlikely to throw - Keep improved error message for debugging when logger is not initialized
Aaronontheweb
commented
Sep 2, 2025
Member
Author
Aaronontheweb
left a comment
There was a problem hiding this comment.
Detailed my changes
| } | ||
|
|
||
| [Fact] | ||
| public void StandardOutWriter_should_handle_concurrent_writes_without_race_conditions() |
Member
Author
There was a problem hiding this comment.
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
| } | ||
|
|
||
| [Fact] | ||
| public void StandardOutWriter_should_handle_null_and_empty_messages() |
Member
Author
There was a problem hiding this comment.
Probably the most useful test of the bunch tbh
| // 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) |
Member
Author
There was a problem hiding this comment.
Comment in the code explains it best
This was referenced Sep 3, 2025
Closed
Aaronontheweb
added a commit
to Aaronontheweb/akka.net
that referenced
this pull request
Sep 4, 2025
…adotnet#7793) * Fix IIS/Windows Service console race condition (akkadotnet#7691) - Detect when running in IIS/Windows Service environments where Console.Out and Console.Error are redirected to the same StreamWriter.Null singleton - Skip console output entirely in these environments to prevent race conditions that cause IndexOutOfRangeException and cascade failures - Improve DefaultLogger error handling to prevent feedback loops - Add unit tests for non-console scenarios The race condition occurs because: 1. IIS/Services redirect both Console.Out and Console.Error to StreamWriter.Null 2. StreamWriter.Null is a singleton, not thread-safe for concurrent access 3. Multiple threads writing to both streams cause IndexOutOfRangeException 4. Console output goes nowhere in these environments anyway Fixes akkadotnet#7691 * Refine console detection and simplify error handling - Make console detection more precise: only skip output when both Console.Out AND Console.Error point to StreamWriter.Null (the exact race condition scenario) - Remove unnecessary try-catch in DefaultLogger.Print() since Tell() is unlikely to throw - Keep improved error message for debugging when logger is not initialized
Aaronontheweb
added a commit
that referenced
this pull request
Sep 5, 2025
* Fix IIS/Windows Service console race condition (#7691) - Detect when running in IIS/Windows Service environments where Console.Out and Console.Error are redirected to the same StreamWriter.Null singleton - Skip console output entirely in these environments to prevent race conditions that cause IndexOutOfRangeException and cascade failures - Improve DefaultLogger error handling to prevent feedback loops - Add unit tests for non-console scenarios The race condition occurs because: 1. IIS/Services redirect both Console.Out and Console.Error to StreamWriter.Null 2. StreamWriter.Null is a singleton, not thread-safe for concurrent access 3. Multiple threads writing to both streams cause IndexOutOfRangeException 4. Console output goes nowhere in these environments anyway Fixes #7691 * Refine console detection and simplify error handling - Make console detection more precise: only skip output when both Console.Out AND Console.Error point to StreamWriter.Null (the exact race condition scenario) - Remove unnecessary try-catch in DefaultLogger.Print() since Tell() is unlikely to throw - Keep improved error message for debugging when logger is not initialized
This was referenced Sep 10, 2025
This was referenced Sep 23, 2025
This was referenced May 21, 2026
Open
Closed
Open
Open
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Changes
The race condition occurs because:
Fixes #7691
Checklist
For significant changes, please ensure that the following have been completed (delete if not relevant):