diff options
author | monojenkins <jo.shields+jenkins@xamarin.com> | 2020-03-26 23:32:18 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-26 23:32:18 +0300 |
commit | d2be7389b11be1c1b65fa42bfb0ed7fa47e58474 (patch) | |
tree | 143b8fa69613eb47365d24810fdd33bf3d438fb3 /mcs/class | |
parent | a7e29665d246d359a778bf4d692b4cb5fd2f3548 (diff) |
[2020-02] [corlib] Suppress finalization of underlying console FileStreams (#19164)
* [corlib] Suppress finalization of underlying console FileStreams
We already suppress the finalization of the CStreamReader and CStreamWriter,
but during shutdown, the underlying FileStream may be finalized which
invalidates its access field which can lead to a crash.
The issue is that during shutdown, the CStreamWriter and its underlying
FileStream might be finalized in arbitrary order, so if the FileStream is
finalized first, even though it doesn't own the safeHandle, the access check in
Write(byte[], int, int) will fail, despite the fact that the underlying call to
MonoIO.Write could have succeeded.
Addresses https://github.com/mono/mono/issues/19005
Co-authored-by: Aleksey Kliger <alklig@microsoft.com>
Diffstat (limited to 'mcs/class')
-rw-r--r-- | mcs/class/corlib/System/Console.cs | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/mcs/class/corlib/System/Console.cs b/mcs/class/corlib/System/Console.cs index 1cb266ad912..0830e4ff079 100644 --- a/mcs/class/corlib/System/Console.cs +++ b/mcs/class/corlib/System/Console.cs @@ -186,7 +186,11 @@ namespace System { try { // TODO: Should use __ConsoleStream from reference sources - return new FileStream (handle, access, false, bufferSize, false, true); + var stream = new FileStream (handle, access, false, bufferSize, false, true); + // Don't run the finalizer on the underlying stream so that System.WriteLine can be + // called inside a finalizer during shutdown or domain unload. + GC.SuppressFinalize (stream); + return stream; } catch (IOException) { return Stream.Null; } |