twittner/zeromq-haskell — Issue #13: Dropped Messages with Pair Sockets
Author: mkscrg
Created: 2012-01-23 17:00:38+00:00
State: open

I just posted this same issue on the zeromq-dev mailing list: http://permalink.gmane.org/gmane.network.zeromq.devel/12954

I was trying to write a simple throughput test for Pair sockets and came across some bad behavior. Here's the code:

import Control.Monad (foldM)
import Data.ByteString.Char8 (pack, unpack)
import Data.Time.Clock.POSIX (getPOSIXTime)
import System.ZMQ3

-- Load in two ghci windows. Run `puller` in one and `pusher 1000` in the
-- other. The 1000th message seems to never arrive. `pusher n` where n < 1000
-- works fine.

pusher :: Int -> IO ()
pusher n = withContext 1 $ \cont -> withSocket cont Pair $ \sock -> do
bind sock "tcp://*:5555"
send sock [] $ pack $ show n
foldM (go sock) () ([1..n]::[Int])
where
go sock _ i = send sock [] $ pack $ show i

puller :: IO ()
puller = withContext 1 $ \cont -> withSocket cont Pair $ \sock -> do
connect sock "tcp://localhost:5555"
n <- fmap (read . unpack) $ receive sock
t1 <- getPOSIXTime
sequence_ $ replicate n $ receive sock >>= putStrLn . unpack
t2 <- getPOSIXTime
putStrLn $ show $ t2 - t1

Like the comment says, running `puller` and `pusher 1000` in two `ghci` sessions causes the puller to hang while waiting for the 1000th message. (The pusher runs and exits, as expected.) The same thing happens with other values >= 1000. With values < 1000, the puller finishes and prints the time as expected.

Thoughts?

Cheers,
Mike