Apparent race condition causing errno 88: "Socket operation on non-socket"

Greetings,

I'm the author of ptGHCi, which uses zeromq4-haskell for messaging between Haskell and Python threads.

I've been getting the captioned error at various points after binding a Rep socket. The code below reproduces the issue for me, but whether it occurs or not seems exquisitely sensitive to details such as the length of the threadDelay (exception is thrown with a delay of 500ms, but not with 100ms) and whether or not profiling is on (I never get the exception in a profiling build -- stack build --profile).

The precise exception is ZMQError { errno = 88, source = "getCStrOpt", message = "Socket operation on non-socket" }.

I'm running under stack (LTS 13.17 resolver, with zeromq-haskell version 0.8.0), Arch linux and zeromq version 4.3.1.

data Sockets = Sockets 
  { requestSock :: Socket Rep
  }


someFunc :: IO ()
someFunc = do
  putStrLn $ "Creating sockets"
  withContext $ \ctx -> do
    requester <- socket ctx Rep
    bind requester "tcp://127.0.0.1:*"
    let sockets = Sockets requester

    sockAddr <- lastEndpoint $ requestSock sockets
    print sockAddr
    threadDelay 1000000
    endpoint <- lastEndpoint $ requestSock sockets -- EXCEPTION THROWN HERE
    putStrLn $ "ZeroMQ endpoint is: " ++ show endpoint

edit: GHC compilation options are -threaded -rtsopts -with-rtsopts=-N. Turning off threading does seem to prevent the issue -- but I don't see why this should be the case. I know ZMQ is not thread safe but the code above shouldn't cause any multithreaded socket access.

Edited Apr 25, 2019 by RichardWarfield
Assignee Loading
Time tracking Loading