joachifm/libmpd-haskell — Issue #8: Problem with multiple MPD requests in the same client
Author: Kanisterschleife
Created: 2012-01-28 09:45:47+00:00
State: open

I tested libmpd with the following program, invoking the next call every second:

import Network.MPD as M
import Control.Concurrent (threadDelay)

main :: IO ()
main = do
M.withMPD M.next
threadDelay 1000000
main

Problem: This works fine for a few calls and then freezes.

Suggestion: strace shows that a new sockets are created on each call without being closed afterwards. It seems that the too many open sockets then finally cause the problem. Replacing

withMPDEx host port pw x = withSocketsDo $
runReaderT (evalStateT (runErrorT . runMPD $ open >> x) initState)
(host, port)
where initState = MPDState Nothing pw (0, 0, 0)

by

withMPDEx host port pw x = withSocketsDo $
runReaderT (evalStateT (runErrorT . runMPD $ (keepResult (mpdOpen >> x) mpdClose)) initState)
(host, port)
where initState = MPDState Nothing pw (0, 0, 0)
keepResult :: (Monad m) => m a -> m b -> m a
keepResult x y = x >>= (\r -> y >> return r)

in Core.hs seems to fix this.