diff --git a/src/libraries/System.Console/src/System/Console.cs b/src/libraries/System.Console/src/System/Console.cs index cecef0a7e5b1f..9a297e6c36b0b 100644 --- a/src/libraries/System.Console/src/System/Console.cs +++ b/src/libraries/System.Console/src/System/Console.cs @@ -235,16 +235,16 @@ static TextWriter EnsureInitialized() private static TextWriter CreateOutputWriter(Stream outputStream) { - return TextWriter.Synchronized(outputStream == Stream.Null ? - StreamWriter.Null : - new StreamWriter( + return outputStream == Stream.Null ? + TextWriter.Null : + TextWriter.Synchronized(new StreamWriter( stream: outputStream, encoding: OutputEncoding.RemovePreamble(), // This ensures no prefix is written to the stream. bufferSize: WriteBufferSize, leaveOpen: true) - { - AutoFlush = true - }); + { + AutoFlush = true + }); } private static StrongBox? _isStdInRedirected; @@ -696,7 +696,15 @@ public static void SetOut(TextWriter newOut) { ArgumentNullException.ThrowIfNull(newOut); - newOut = TextWriter.Synchronized(newOut); + // Ensure all access to the writer is synchronized. If it's the known Null + // singleton writer, which may be used if someone wants to suppress all + // console output, we needn't add synchronization because all operations + // are nops. + if (newOut != TextWriter.Null) + { + newOut = TextWriter.Synchronized(newOut); + } + lock (s_syncObject) { s_isOutTextWriterRedirected = true; @@ -708,7 +716,12 @@ public static void SetError(TextWriter newError) { ArgumentNullException.ThrowIfNull(newError); - newError = TextWriter.Synchronized(newError); + // Ensure all access to the writer is synchronized. See comment in SetOut. + if (newError != TextWriter.Null) + { + newError = TextWriter.Synchronized(newError); + } + lock (s_syncObject) { s_isErrorTextWriterRedirected = true;