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 }}
Linux Sockets: Close with pending synchronous Receive hangs #22901
If you issue a blocking Receive on a connected socket, and then try to Close it from another thread, the Close call will hang and the Receive will never complete.
This works properly on Windows -- the Close succeeds and the Receive throws a SocketException with errorCode = ConnectionAborted.
I should have been clearer. I don't necessarily expect the Receive call to complete; that's OS dependent.
The specific problem here is that we're hanging in the Close path trying to close the AsyncContext, because of some deadlock in AsyncContext's close logic.
If I just disable the AsyncContext close call, then the Close does not hang and the Receive never completes, which is what we expect on Linux, I believe.
The specific problem here is that we're hanging in the Close path trying to close the AsyncContext, because of some deadlock in AsyncContext's close logic.
Ah. What happens if you issue a shutdown before closing the handle... does that help at all?
Calling shutdown() before close() seems to cause the sync Receive to return 0 bytes (i.e. EOF).
This certainly seems better than hanging forever. I suppose we could try to detect that we're in the process of closing and convert this into an exception, like Windows does. Or we could just leave it returning 0 bytes.
That said, calling shutdown made a few other tests break which I have not investigated.
FYI, for sync Send the behavior is similar: without shutdown it hangs, with shutdown it returns EINVAL -- presumably because the socket is no longer valid for sending.
I found this also hangs on Windows on a single processor box. The underlying system call choices are undefined here. You might have to force the single processor kernel to be used; for some reason it always uses the multi-processor kernel on VM boxes.
This may be similar to dotnet/corefx#26034. In this case there is pending blocking accept() system call.
The current code does not get to actually closing socket and it hangs. calling shutdown before TcpListener.stop() also fixes the hang. (and accept() will fail and come back)
For dotnet/corefx#26034 I was not sure if that is real bug of if it is expected that blocking call would actually block.
Can we possibly track pending blocking operations and call shutdown as needed?
Activity
geoffkizer commentedon Jul 24, 2017
Also note, this works fine with an asynchronous Receive.
stephentoub commentedon Jul 24, 2017
I believe it's undefined behavior in BSD sockets. Windows makes that work, but we can't depend on it working on arbitrary systems.
geoffkizer commentedon Jul 25, 2017
I should have been clearer. I don't necessarily expect the Receive call to complete; that's OS dependent.
The specific problem here is that we're hanging in the Close path trying to close the AsyncContext, because of some deadlock in AsyncContext's close logic.
If I just disable the AsyncContext close call, then the Close does not hang and the Receive never completes, which is what we expect on Linux, I believe.
stephentoub commentedon Jul 25, 2017
Ah. What happens if you issue a shutdown before closing the handle... does that help at all?
geoffkizer commentedon Jul 25, 2017
Haven't tried that (I will later).
geoffkizer commentedon Jul 25, 2017
Calling shutdown() before close() seems to cause the sync Receive to return 0 bytes (i.e. EOF).
This certainly seems better than hanging forever. I suppose we could try to detect that we're in the process of closing and convert this into an exception, like Windows does. Or we could just leave it returning 0 bytes.
That said, calling shutdown made a few other tests break which I have not investigated.
geoffkizer commentedon Jul 25, 2017
FYI, for sync Send the behavior is similar: without shutdown it hangs, with shutdown it returns EINVAL -- presumably because the socket is no longer valid for sending.
jhudsoncedaron commentedon Jan 15, 2018
I found this also hangs on Windows on a single processor box. The underlying system call choices are undefined here. You might have to force the single processor kernel to be used; for some reason it always uses the multi-processor kernel on VM boxes.
wfurt commentedon Jan 19, 2018
This may be similar to dotnet/corefx#26034. In this case there is pending blocking accept() system call.
The current code does not get to actually closing socket and it hangs. calling shutdown before TcpListener.stop() also fixes the hang. (and accept() will fail and come back)
For dotnet/corefx#26034 I was not sure if that is real bug of if it is expected that blocking call would actually block.
Can we possibly track pending blocking operations and call shutdown as needed?
fix windows build and add unit test for #22564 scenario.
fix windows build and add unit test for #22564 scenario.
davidsh commentedon Jun 21, 2019
Reactivating since we reverted PR dotnet/corefx#37486.