-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Socket.SendFile should fail if when Socket.Blocking = false #47287
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsIf you put a socket into nonblocking mode (i.e. set Socket.Blocking=false) and then call synchronous SendFile, you get unexpected behavior. On Windows, the call will block but will ultimately succeed. SendFile is not intended to be used in nonblocking mode. If a user attempts this we should throw InvalidOperationException. More details here: #47230 (review)
|
Triage: We should throw as suggested above. Should be fairly straightforward to do. |
Just to understand it better this bug fix would be checking for blocking =false in every synchronous method and throw in case of it is false need to throw invalid operation exception , is my understanding towards this bug is correct? |
On Linux this should work.
|
Yeah, my comment above is wrong/confusing. I should have said, on Linux, the kernel will buffer as much of the file as it can/wants to, and then tell you how much it sent, just like any other send operation. This will likely result in EWOULDBLOCK if you need to call sendfile multiple times, but that's not the real problem. The problem is, Socket.SendFile doesn't return a value telling you how much it actually sent, like Socket.Send does. So there's no way for the user to know how much of the file was actually sent, which makes the API pretty useless. We could in theory add a new method that returns the bytesSent. But there's no obvious way to make this work on Windows, since Windows doesn't seem to actually respect the non-blocking state of the socket for TransmitFile. I think the conclusion still stands: we should just fail Socket.SendFile when Socket.Blocking = false. |
In what case in particular? It looks like we are doing the retry logic in SocketAsyncContext. |
No, this is specifically about SendFile. |
ok so i need to apply blocking =false check for specific method of send file and this will fix this bug , asking because i want to make pr , so before that i need to be clear about what actually case of bug fix, Thanks |
SendFile in when Socket.Blocking = false. |
We're not looping on partial sends like we do for runtime/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs Lines 883 to 885 in 4df29c9
|
We are looping in TryCompleteSendFile: runtime/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs Line 935 in 4df29c9
|
Ah yes. Is there a test that occasionally fails on Linux that I can run? |
I don't think there's an existing test, but there's discussion of what tests we should have here: #47230 (review) @antonfirsov Did we add a (disabled) test for this? |
Hi,
try
{
if(Socket.Binding == false)
{
sent = SendFile(socket, handle, ref offset, ref count, out errno);
bytesSent += sent;
}
}
|
If you put a socket into nonblocking mode (i.e. set Socket.Blocking=false) and then call synchronous SendFile, you get unexpected behavior.
On Windows, the call will block but will ultimately succeed.
(EDIT: See my comment below for clarification.) On Linux, you will likely end up with an EWOULDBLOCK result, but this isn't not reliable; sometimes the call will succeed, sometimes it will send partial data, etc.
SendFile is not intended to be used in nonblocking mode. If a user attempts this we should throw InvalidOperationException.
More details here: #47230 (review)
The text was updated successfully, but these errors were encountered: