Skip to content

Writing to StreamWriter.Null can cause exceptions #98457

@AndyAyersMS

Description

@AndyAyersMS

This surprised me as I thought a null stream was stateless and did nothing...

string longLine = new string('#', 100000);
Parallel.For(0, 25, i => StreamWriter.Null.WriteLine(longLine));

produces

Unhandled exception. System.AggregateException: One or more errors occurred. (Arithmetic operation resulted in an overflow.) (Arithmetic operation resulted in an overflow.) (Specified argument was out of the range of valid values.) (Arithmetic operation resulted in an overflow.) (Arithmetic operation resulted in an overflow.) (Arithmetic operation resulted in an overflow.) (Arithmetic operation resulted in an overflow.) (Arithmetic operation resulted in an overflow.) (Specified argument was out of the range of valid values.) (Specified argument was out of the range of valid values.) (Arithmetic operation resulted in an overflow.) (Arithmetic operation resulted in an overflow.) (Specified argument was out of the range of valid values.)
 ---> System.OverflowException: Arithmetic operation resulted in an overflow.
   at System.IO.StreamWriter.WriteLine(String value)
   at Program.<>c__DisplayClass0_0.<<Main>$>b__0(Int32 i) in D:\bugs\streaming-spmi\null\Program.cs:line 4
   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
   at System.Threading.Tasks.TaskReplicator.Replica.Execute()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.TaskReplicator.Run[TState](ReplicatableUserAction`1 action, ParallelOptions options, Boolean stopOnFirstFailure)
   at System.Threading.Tasks.Parallel.ForWorker[TLocal,TInt](TInt fromInclusive, TInt toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Parallel.ForWorker[TLocal,TInt](TInt fromInclusive, TInt toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, Action`1 body)
   at Program.<Main>$(String[] args) in D:\bugs\streaming-spmi\null\Program.cs:line 4
 ---> (Inner Exception #1) System.OverflowException: Arithmetic operation resulted in an overflow.
   at System.IO.StreamWriter.WriteLine(String value)
   at Program.<>c__DisplayClass0_0.<<Main>$>b__0(Int32 i) in D:\bugs\streaming-spmi\null\Program.cs:line 4
   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
   at System.Threading.Tasks.TaskReplicator.Replica.Execute()<---

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions