Skip to content

Add reliable timeouts to socket operations #17711

Open
@GSPP

Description

@GSPP

Socket operations do not support timeouts sufficiently in the following cases:

  1. All async operations do not respect the configured timeout.
  2. Connect cannot be made to observe a timeout. I think there is no timeout for Accept either but that's usually an operation that is never supposed to time out.

The workarounds usually are quite nasty. For (1) people build their own (tedious and often flawed) timeouts. For (2) people often use an event plus an async IO. In case the timeout fires they close the socket. (This actually triggers this unavoidable race condition resulting in an access violation!)

Note the amount of confusion that these problems cause: https://www.google.com/webhp?complete=1&hl=en&gws_rd=cr,ssl&ei=#complete=1&hl=en&q=socket+beginreceive+timeout (I sometimes like to point to Google and Stack Overflow to show a nice sample of real-world user complaints. I have opened this ticket because I'm repeatedly facing this issue as well.)

I understand that the underlying Winsock (and now Linux) APIs make some of this hard. Since these issues are so widespread I believe it's worth the work to fix this at the managed code level (by implementing the timeouts and aborts in .NET code as opposed to making the native APIs do that).

As an open concern for (1) I see the question what happens to an IO that is cancelled at the managed level due to timeout or cancellation but still running at the native level. This can cause data to be discarded. The only clean way to solve this that I could find is to terminate the connection when a read or write ends up being cancelled. In my experience this matches perfectly with real world requirements. In case of a timeout usually the calling code backs out and just wants to shut everything down.

To summarize the ticket: Please add timeouts to all possibly blocking socket operations. Related ticket: Allow the user to cancel anything at will (this is needed in addition to the timeout).. Also related: Add Task-based async methods which should take a CancellationToken and observe the configured timeout.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Net.SocketsenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions