You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FileStream.ReadAsync() with isAsync: true is throwing IOException #54143
Exception has occurred: CLR/System.IO.IOException
An exception of type 'System.IO.IOException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Incorrect function. : '\\?\hid#vid_28bd&pid_0906&mi_02#7&194ad48b&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}''
at System.IO.Strategies.FileStreamHelpers.GetFileLength(SafeFileHandle handle, String path)
at System.IO.Strategies.WindowsFileStreamStrategy.get_Length()
at System.IO.Strategies.AsyncWindowsFileStreamStrategy.ReadAsyncInternal(Memory`1 destination, CancellationToken cancellationToken)
at System.IO.Strategies.AsyncWindowsFileStreamStrategy.ReadAsync(Memory`1 destination, CancellationToken cancellationToken)
at System.IO.Strategies.BufferedFileStreamStrategy.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
at System.IO.FileStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
The source repo is currently private, but if more information or testing is needed from my side then I'll do it willingly.
Exception has occurred: CLR/System.IO.IOException
An exception of type 'System.IO.IOException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Incorrect function. : '\\?\hid#vid_28bd&pid_0906&mi_02#7&194ad48b&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}''
at System.IO.Strategies.FileStreamHelpers.GetFileLength(SafeFileHandle handle, String path)
at System.IO.Strategies.WindowsFileStreamStrategy.get_Length()
at System.IO.Strategies.AsyncWindowsFileStreamStrategy.ReadAsyncInternal(Memory`1 destination, CancellationToken cancellationToken)
at System.IO.Strategies.AsyncWindowsFileStreamStrategy.ReadAsync(Memory`1 destination, CancellationToken cancellationToken)
at System.IO.Strategies.BufferedFileStreamStrategy.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
at System.IO.FileStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
I'm running Windows 10 21H1, the latest one, and the file is a local HID device found through SetupDi functions.
I've set the environment variable on either the cmd prompt or globally like this;
Then I rebuilt it just to be sure, but both still failed with this stack trace:
Exception has occurred: CLR/System.IO.IOException
An exception of type 'System.IO.IOException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Incorrect function. : '\\?\hid#vid_28bd&pid_0906&mi_02#7&194ad48b&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}''
at System.IO.Strategies.FileStreamHelpers.GetFileLength(SafeFileHandle handle, String path)
at System.IO.Strategies.WindowsFileStreamStrategy.get_Length()
at System.IO.Strategies.AsyncWindowsFileStreamStrategy.ReadAsyncInternal(Memory`1 destination, CancellationToken cancellationToken)
at System.IO.Strategies.AsyncWindowsFileStreamStrategy.ReadAsync(Memory`1 destination, CancellationToken cancellationToken)
at System.IO.Strategies.BufferedFileStreamStrategy.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
at System.IO.FileStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
If that's the exact stack trace you saw in both cases, then the environment variable didn't properly configure things. Can you share the exact stack trace you got with the environment variable set?
@X9VoiD big thanks for providing all the information. Looking at the results you have provided we have most probably never supported this scenario (GetFileType() returns 0 which means UNKNOWN file type) but I think that we should be able to get it working by recognizing such files as non-seekable files. The problem is that I can't repro the problem locally...
Is there any chance that you could tell me which sys-calls do you use exactly to get such a path? Or help me to create a repro? This is what I got so far:
usingSystem;usingSystem.ComponentModel;usingSystem.Runtime.InteropServices;usingSystem.Text;namespaceDeviceIds{classProgram{staticvoidMain(string[]args){constintDIGCF_PRESENT=0x2;constintDIGCF_DEVICEINTERFACE=0x10;constintERROR_NO_MORE_ITEMS=259;HidD_GetHidGuid(outGuidHidGuid);IntPtrdeviceInfoSet=SetupDiGetClassDevs(inHidGuid,IntPtr.Zero,IntPtr.Zero,DIGCF_PRESENT|DIGCF_DEVICEINTERFACE);try{SP_DEVINFO_DATAdeviceInfoData=newSP_DEVINFO_DATA();deviceInfoData.cbSize=(uint)Marshal.SizeOf(deviceInfoData);uintdeviceIndex=0;while(SetupDiEnumDeviceInfo(deviceInfoSet,deviceIndex++,refdeviceInfoData)){if(Marshal.GetLastWin32Error()==ERROR_NO_MORE_ITEMS)break;SP_DEVICE_INTERFACE_DATAdeviceInterfaceData=newSP_DEVICE_INTERFACE_DATA();deviceInterfaceData.cbSize=Marshal.SizeOf(deviceInterfaceData);if(!SetupDiEnumDeviceInterfaces(deviceInfoSet,IntPtr.Zero,inHidGuid,deviceIndex,refdeviceInterfaceData)){// throw new Win32Exception("Failed to get device interface data");}SP_DEVICE_INTERFACE_DETAIL_DATAdeviceInterfaceDetailData=newSP_DEVICE_INTERFACE_DETAIL_DATA();deviceInterfaceDetailData.cbSize=IntPtr.Size==8?8:6;uintsize=(uint)Marshal.SizeOf(deviceInterfaceDetailData);if(!SetupDiGetDeviceInterfaceDetail(deviceInfoSet,refdeviceInterfaceData,refdeviceInterfaceDetailData,size,refsize,IntPtr.Zero)){// throw new Exception("Failed to get device interface detail");}Console.WriteLine(deviceInterfaceDetailData.DevicePath);}}finally{SetupDiDestroyDeviceInfoList(deviceInfoSet);}}[StructLayout(LayoutKind.Sequential)]structSP_DEVICE_INTERFACE_DATA{publicintcbSize;publicGuidinterfaceClassGuid;publicintflags;privateUIntPtrreserved;}[StructLayout(LayoutKind.Sequential)]structSP_DEVINFO_DATA{publicuintcbSize;publicGuidClassGuid;publicuintDevInst;publicIntPtrReserved;}[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]structSP_DEVICE_INTERFACE_DETAIL_DATA{publicintcbSize;[MarshalAs(UnmanagedType.ByValTStr,SizeConst=256)]publicstringDevicePath;}[DllImport("setupapi.dll",CharSet=CharSet.Auto)]staticexternIntPtrSetupDiGetClassDevs(IntPtrClassGuid,[MarshalAs(UnmanagedType.LPTStr)]stringEnumerator,IntPtrhwndParent,intFlags);[DllImport("setupapi.dll",CharSet=CharSet.Auto)]staticexternIntPtrSetupDiGetClassDevs(inGuidClassGuid,IntPtrEnumerator,IntPtrhwndParent,intFlags);[DllImport("setupapi.dll",SetLastError=true)]staticexternboolSetupDiDestroyDeviceInfoList(IntPtrDeviceInfoSet);[DllImport("setupapi.dll",SetLastError=true)]staticexternboolSetupDiEnumDeviceInfo(IntPtrDeviceInfoSet,uintMemberIndex,refSP_DEVINFO_DATADeviceInfoData);[DllImport("setupapi.dll",SetLastError=true,CharSet=CharSet.Auto)]staticexternboolSetupDiGetDeviceInstanceId(IntPtrDeviceInfoSet,refSP_DEVINFO_DATADeviceInfoData,StringBuilderDeviceInstanceId,intDeviceInstanceIdSize,outintRequiredSize);[DllImport("setupapi.dll",CharSet=CharSet.Unicode,SetLastError=true)]staticexternboolSetupDiEnumDeviceInterfaces(IntPtrhDevInfo,IntPtrdevInfo,inGuidinterfaceClassGuid,uintmemberIndex,refSP_DEVICE_INTERFACE_DATAdeviceInterfaceData);[DllImport("setupapi.dll",CharSet=CharSet.Unicode,SetLastError=true)]staticexternboolSetupDiGetDeviceInterfaceDetail(IntPtrhDevInfo,refSP_DEVICE_INTERFACE_DATAdeviceInterfaceData,refSP_DEVICE_INTERFACE_DETAIL_DATAdeviceInterfaceDetailData,uintdeviceInterfaceDetailDataSize,refuintrequiredSize,IntPtrdeviceInfoData);[DllImport("hid.dll",SetLastError=true)]staticexternvoidHidD_GetHidGuid(outGuidGuid);}}
Activity
adamsitnik commentedon Jun 14, 2021
Hi @X9VoiD
Are you sure that you have provided the right example and stack trace?
In the source code I can see a call to
ReadAsyncWhile in the stack trace I can see a call to
Read(sync, not async):The method that should be invoked:
runtime/src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
Line 358 in 8a9a24b
X9VoiD commentedon Jun 14, 2021
My bad, here's the correct stack trace:
Also updated the stack trace in issue.
adamsitnik commentedon Jun 14, 2021
@X9VoiD Could you please try to run your example with the following env var enabled:
And see if it works?
Which Windows version are you using? Is the file a local file or a file located on a remote machine? What is the file system?
X9VoiD commentedon Jun 14, 2021
I'm running Windows 10 21H1, the latest one, and the file is a local HID device found through SetupDi functions.
I've set the environment variable on either the cmd prompt or globally like this;

Then I rebuilt it just to be sure, but both still failed with this stack trace:
stephentoub commentedon Jun 14, 2021
If that's the exact stack trace you saw in both cases, then the environment variable didn't properly configure things. Can you share the exact stack trace you got with the environment variable set?
stephentoub commentedon Jun 14, 2021
Does it work for you with .NET 5 or any other version of .NET besides .NET 6?
adamsitnik commentedon Jun 15, 2021
@X9VoiD could you please run the following app by providing the path as first argument?
dotnet run -- "thePathGoesHere"X9VoiD commentedon Jun 15, 2021
adamsitnik commentedon Jun 23, 2021
@X9VoiD big thanks for providing all the information. Looking at the results you have provided we have most probably never supported this scenario (
GetFileType()returns0which meansUNKNOWNfile type) but I think that we should be able to get it working by recognizing such files as non-seekable files. The problem is that I can't repro the problem locally...Is there any chance that you could tell me which sys-calls do you use exactly to get such a path? Or help me to create a repro? This is what I got so far:
Which gives me a list of device IDs:
But I don't know how to get a path to a file like yours.
X9VoiD commentedon Jun 23, 2021
This gives me:
Which should be a valid path. Some of these file paths though are exclusively opened by Windows, for example, all of the paths ending in
\kbdadamsitnik commentedon Jun 24, 2021
@X9VoiD big thanks for providing the repro!
I was able to confirm that it was not a regression, because it was throwing the same exception for .NET 5:
The good news is that it works with latest .NET 6 preview as it got fixed by #53669
Some explanation:
Now if
GetFileType()returnsUNKNOWN(0):runtime/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Lines 224 to 238 in 7833828
SafeFileHandle.CanSeekreturnsfalse:runtime/src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Windows.cs
Line 30 in 7833828
And because of that we don't access the problematic
GetFileLength:runtime/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/AsyncWindowsFileStreamStrategy.cs
Lines 47 to 55 in 7833828
I am going to add a test that ensures that it keeps working.
@X9VoiD in theory it should work with the Preview 6 bits but it should definitely work with latest bits. The links come from https://github.com/dotnet/installer
adamsitnik commentedon Jul 1, 2021
Since this works thanks to #53669 and I've added tests to ensure it keeps working in #54673, I am closing the issue.
@X9VoiD once again big thanks for all the help with creating the repro!