Skip to content

IORef based Socket is unsafe #335

Description

@winterland1989

Peek through recently network's code, the new IORef CInt based Socket seems to still have previous discussed fd-reuse race condiftion:

  1. Thread A acquires a Socket 's fd by fdSocket, when it's about to do some operations, the scheduler decide to pause thread A.

  2. Thread B close the socket, rewrite the Socket 's IORef 's content.

  3. Thread C open a new socket, unfortunately got the same fd number thread A is holding now.

  4. When thread A is resumed, it's unaware of thread B and thread C 's behavior, thus the following operations will performed successfully, but on a wrong fd.

Maybe it's necessary to add some docs on close ?

Activity

  1. kazu-yamamoto commented on Jun 26, 2018

    @kazu-yamamoto
    Collaborator

    @winterland1989 Would you suggest text for doc on close?

  2. winterland1989 commented on Jun 26, 2018

    @winterland1989
    Author

    Maybe something like this?

    -- | Close the socket. Sending data to or receiving data from closed socket
    --   may lead to undefined behaviour.
    --
    -- Note: this function is not thread-safe, it may not abort concurrently read or write 
    -- operations, in fact other threads may continue to work on a fd reused by OS.
    --
    close :: Socket -> IO ()
    ...
    
  3. kazu-yamamoto commented on Jul 2, 2018

    @kazu-yamamoto
    Collaborator

    The document is written in '2.7' and 'master'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions