Fix missing error handling in SafeFileHandle.Type on Windows#126618
Fix missing error handling in SafeFileHandle.Type on Windows#126618adamsitnik merged 4 commits intomainfrom
Conversation
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/4f6aad61-177e-46ba-af07-c0835e31e477 Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
|
Tagging subscribers to this area: @dotnet/area-system-io |
adamsitnik
left a comment
There was a problem hiding this comment.
@copilot add a test that creates an invalid SafeFileHandle with a positive handle value and ensures that Type throws the same exception on every platform. One idea would be to open a regular file with File.OpenHandle, then create a copy of the handle then close the original handle and then call the Type property for the copy
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/18b0964e-a520-4329-add2-d02761a087ec Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Added |
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/SafeFileHandle/GetFileType.cs
Outdated
Show resolved
Hide resolved
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/3a0338b4-3054-4780-aa40-a53037e9e9d8 Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes Windows-specific behavior in SafeFileHandle.Type so that invalid handles no longer silently report FileHandleType.Unknown; instead, the implementation checks GetLastError when GetFileType returns FILE_TYPE_UNKNOWN and throws a Win32-based exception when an actual error occurred.
Changes:
- Add a pre-switch
FILE_TYPE_UNKNOWNbranch inGetFileTypeCore()to distinguish “unknown type” from “error” viaMarshal.GetLastPInvokeError(). - Throw
Win32Marshal.GetExceptionForWin32Error(...)whenGetLastError != ERROR_SUCCESS; otherwise returnFileHandleType.Unknown.
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Show resolved
Hide resolved
@jkotas what if we put this test in a dedicated class and annotate it with following attribute that ensures other tests are not running in parallel? Can runtime still open some new handles/descriptors in the meantime? |
Yes, you have GC threads, finalizer thread, tiered compilation thread, debugger thread, diagnostic server thread, and several PAL threads all running in the background doing stuff that you do not have a control over. |
|
/ba-g the failures are unrelated |
…126618) Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Description
SafeFileHandle.Typesilently returnsFileHandleType.Unknownon Windows when called with an invalid handle, instead of throwing an exception. This is becauseGetFileTypereturnsFILE_TYPE_UNKNOWN(0) for both "type is genuinely unknown" and "an error occurred" — the distinction requires checkingGetLastError.Fix: In
GetFileTypeCore(), handleFILE_TYPE_UNKNOWNbefore the switch expression. CheckMarshal.GetLastPInvokeError()and throw a Win32 exception if the error code is non-zero; only returnFileHandleType.UnknownwhenGetLastErrorisERROR_SUCCESS.Changes
SafeFileHandle.Windows.cs— extractFILE_TYPE_UNKNOWNcheck before switch inGetFileTypeCore(), addGetLastErrorcheck to throw on error