add missing GC.SuppressFinalize(this) to FileStream.DisposeAsync#65899
add missing GC.SuppressFinalize(this) to FileStream.DisposeAsync#65899adamsitnik merged 6 commits intodotnet:mainfrom
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsI was unable to repro #65835 locally for a few hours, but I believe that the it's caused by lack of Explanation: the default runtime/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs Lines 176 to 180 in 95f7f7a which calls runtime/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs Lines 158 to 167 in 95f7f7a
In #65835 we can see that the buffer was actually written to disk twice. I guess (I was not able to repro it) that it's caused by a flush implemented for finalizer. I am not 100% sure because the finally block sets write position to 0: So after I've tried really hard to write a failing unit test, but I've failed. The reason for that is that all custom types deriving from which does call the base impl and is bug free.
|
src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
Outdated
Show resolved
Hide resolved
…eam.BaseDisposeAsync, as FileStream.DisposeAsync calls Dispose(false) now
I was unable to repro #65835 locally for a few hours, but I believe that the it's caused by lack of
GC.SuppressFinalize(this)inFileStream.DisposeAsyncand my recent changes from #64997 have just exposed the problem by adding the finalizer toFileStreamitself.Explanation: the default
Stream.DisposeAsynccallsDispose:runtime/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs
Lines 176 to 180 in 95f7f7a
which calls
Closewhich callsGC.SuppressFinalize(this):runtime/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs
Lines 158 to 167 in 95f7f7a
FileStreamwas overridingDisposeAsync, but not callingGC.SuppressFinalize(this).runtime/src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
Line 500 in 95f7f7a
In #65835 we can see that the buffer was actually written to disk twice. I guess (I was not able to repro it) that it's caused by a flush implemented for finalizer. I am not 100% sure because the finally block sets write position to 0:
runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs
Lines 115 to 121 in 95f7f7a
So after
DisposeAsyncthe flushing should in theory see that there is nothing to flush. Moreover, it should also observe that the handle was already closed.runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/BufferedFileStreamStrategy.cs
Lines 125 to 130 in 95f7f7a
I've tried really hard to write a failing unit test, but I've failed. The reason for that is that all custom types deriving from
FileStreamare callingBaseDisposeAsync:runtime/src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
Line 579 in 95f7f7a
which does call the base impl and is bug free.