-
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
Fix IIS/Windows Service console race condition (#7691) #7793
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
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.
Detailed my changes
} | ||
|
||
[Fact] | ||
public void StandardOutWriter_should_handle_concurrent_writes_without_race_conditions() |
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
} | ||
|
||
[Fact] | ||
public void StandardOutWriter_should_handle_null_and_empty_messages() |
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.
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) |
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.
Comment in the code explains it best
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.
LGTM
…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
* 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
Changes
The race condition occurs because:
Fixes #7691
Checklist
For significant changes, please ensure that the following have been completed (delete if not relevant):