[Haskell-cafe] using createProcess and waitForProcess, but mpg123 plays for only 6 seconds

Herbert Valerio Riedel hvr at gnu.org
Thu Nov 15 15:42:38 CET 2012

rnons <remotenonsense at gmail.com> writes:


> mpgLoop = do
>     let sh = "mpg123 -R"
>     (Just hin, Just hout, _, hdl) <- createProcess (shell sh){ std_in = CreatePipe, std_out=CreatePipe }
>     --hPutStrLn hin "SILENCE"
>     hPutStrLn hin "LOAD /home/rnons/Music/test.mp3"
>     hFlush hin
>     waitForProcess hdl
>     return ()


> I expected with "waitForProcess", this program will be able to run
> till the song ends. However, mpg123 plays for only 6 seconds.
> Maybe I missed something?

Just a guess: the problem may be that "a Handle will be automatically
closed when the garbage collector detects that it has become
unreferenced by the program"[1], and in the program above, the liveness
of the hin/hout references ends before mpg123 is finished playing. And
'mpg123 -R' shuts down as soon as it detects its stdin (or stdout)
getting EOF... so that's what you're experiencing most likely here.

Try to keep hin/hout "alive" a bit longer, and mpg123 should survive
longer. Also you should most probably consume the output comming from
'mpg123 -R' via 'hout', as otherwise it the buffer might build up and
mpg123 might block.


 [1]: http://hackage.haskell.org/packages/archive/base/

More information about the Haskell-Cafe mailing list