Skip to content

Linux Sockets: Close with pending synchronous Receive hangs #22901

Closed

Description

@geoffkizer

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.

Activity

  1. geoffkizer commented on Jul 24, 2017

    @geoffkizer
    ContributorAuthor

    Also note, this works fine with an asynchronous Receive.

  2. stephentoub commented on Jul 24, 2017

    @stephentoub
    Member

    This works properly on Windows

    I believe it's undefined behavior in BSD sockets. Windows makes that work, but we can't depend on it working on arbitrary systems.

  3. geoffkizer commented on Jul 25, 2017

    @geoffkizer
    ContributorAuthor

    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.

  4. stephentoub commented on Jul 25, 2017

    @stephentoub
    Member

    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?

  5. geoffkizer commented on Jul 25, 2017

    @geoffkizer
    ContributorAuthor

    Haven't tried that (I will later).

  6. geoffkizer commented on Jul 25, 2017

    @geoffkizer
    ContributorAuthor

    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.

  7. geoffkizer commented on Jul 25, 2017

    @geoffkizer
    ContributorAuthor

    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.

  8. jhudsoncedaron commented on Jan 15, 2018

    @jhudsoncedaron

    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.

  9. wfurt commented on Jan 19, 2018

    @wfurt
    Member

    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?

  10. davidsh commented on Jun 21, 2019

    @davidsh
    Contributor

    Reactivating since we reverted PR dotnet/corefx#37486.

  11. transferred this issue fromdotnet/corefxon Jan 31, 2020
  12. added this to the 5.0 milestone on Jan 31, 2020
  13. ghost locked as resolved and limited conversation to collaborators on Dec 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Net.Socketsbugos-linux

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions