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
CsWin32 generates a ReadFileEx and WriteFileEx that take Span<byte>. The generated methods increment the reference count on the handle, pin the span, then invoke the native routines. For asynchronous I/O (which these are designed for), this is incorrect. When the native method returns, two issues occur:
Decrementing the reference count could inadvertently dispose the safe handle
Unpinning the span leaves a GC hold that the underlying I/O driver will be reading or writing against
CsWin32 should at least not generate "nice" Span<byte> overloads for ReadFileEx and WriteFileEx.
If you want to go further, you could provide a Memory<byte> version that handles pinning, and a completion routine that wraps the users' to unpin it. This is what one has to do anyways to properly use these APIs.
Repro steps
NativeMethods.txt content:
ReadFileEx
WriteFileEx
Context
CsWin32 version: 0.3.106
Target Framework: net8.0
LangVersion: Latest
The text was updated successfully, but these errors were encountered:
This sounds like a dupe (in area at least) of #1066.
But the two functions you mention wouldn't be included in the fix for that because they are missing an attribute in the metadata. I filed microsoft/win32metadata#1994 to track that.
CsWin32 generates a
ReadFileEx
andWriteFileEx
that takeSpan<byte>
. The generated methods increment the reference count on the handle, pin the span, then invoke the native routines. For asynchronous I/O (which these are designed for), this is incorrect. When the native method returns, two issues occur:Generated code
Expected behavior
CsWin32 should at least not generate "nice"
Span<byte>
overloads forReadFileEx
andWriteFileEx
.If you want to go further, you could provide a
Memory<byte>
version that handles pinning, and a completion routine that wraps the users' to unpin it. This is what one has to do anyways to properly use these APIs.Repro steps
NativeMethods.txt
content:Context
net8.0
LangVersion
:Latest
The text was updated successfully, but these errors were encountered: