Skip to content

Conversation

@Aaronontheweb
Copy link
Member

Changes

  • 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

…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 Aaronontheweb added logging akka.net v1.6 Akka.NET v1.6-related issues labels Sep 4, 2025
@Aaronontheweb Aaronontheweb enabled auto-merge (squash) September 4, 2025 16:47
@Aaronontheweb Aaronontheweb merged commit 9dab980 into akkadotnet:dev Sep 5, 2025
8 of 11 checks passed
@Aaronontheweb Aaronontheweb deleted the fix/7691-iis-console-race-condition-v1.6 branch September 5, 2025 02:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

akka.net v1.6 Akka.NET v1.6-related issues logging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Akka.Logging - stdout - System.IndexOutOfRangeException: Probable I/O race condition detected

1 participant