<div dir="ltr">You're not reading from the stdout or stderr of the subprocess, so either those handles get garbage collected and closed (as Donn pointed out), which will probably cause mplayer to crash, or the stdout buffer of the mplayer process will fill and further write()s to that file descriptor will block, causing deadlock. Try forking a couple of threads to keep those handles clear.<div>
<br></div><div>G</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, May 17, 2014 at 1:44 AM, Karsten Gebbert <span dir="ltr"><<a href="mailto:k@ioctl.it" target="_blank">k@ioctl.it</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
I'm writing a little networking wrapper around a sub-process (mplayer<br>
-idle -slave) and I'm running into some issues with the System.Process<br>
API. This is the program:<br>
<br>
> module Main where<br>
><br>
> import System.IO<br>
> import System.Process<br>
> import Network.Socket hiding (recv)<br>
> import Network.Socket.ByteString<br>
> import Control.Concurrent<br>
> import qualified Data.ByteString.Char8 as C<br>
><br>
> -- WARNING: when compiled with -threaded, this program is likely not going<br>
> -- to work. As soon as one writes to the stdin of the forked process, it<br>
> -- zombifies and any other command with crash this program.<br>
><br>
> main = withSocketsDo $ do<br>
>     -- network stuff<br>
>     addrinfos <- getAddrInfo Nothing (Just "localhost") (Just "4000")<br>
>     let serveraddr = head addrinfos<br>
>     sock <- socket (addrFamily serveraddr) Stream defaultProtocol<br>
>     bindSocket sock (addrAddress serveraddr)<br>
>     listen sock 1<br>
><br>
>     -- mplayer<br>
>     (hand,o,e,pid) <- runInteractiveProcess "mplayer" ["-fs", "-idle", "-slave"] Nothing Nothing<br>
>     hSetBinaryMode hand False<br>
>     hSetBuffering hand LineBuffering<br>
><br>
>     putStrLn "listening for commands"<br>
>     loop sock hand<br>
><br>
>     -- closing everything down<br>
>     sClose sock<br>
>     terminateProcess pid<br>
>     waitForProcess pid<br>
>     return ()<br>
><br>
> loop sock hand = do<br>
>     (conn, _) <- accept sock<br>
>     str <- recv conn 2048<br>
><br>
>     putStr $ "received: " ++ C.unpack str<br>
><br>
>     -- write command to handler<br>
>     hPutStr hand $ C.unpack str<br>
><br>
>     sClose conn<br>
>     loop sock hand<br>
<br>
<br>
When compile with -threaded, the mplayer process gets zombified and<br>
hangs until I shut down the program. When compiled with non-threaded RTS<br>
(thats whats its called, correct?) I can successfully send a few<br>
commands, but then mplayer freezes. When I strace mplayer, this error is<br>
what it gets stuck on.<br>
<br>
<br>
    ioctl(0, TIOCGWINSZ, 0x7fff2897a070)    = -1 ENOTTY (Inappropriate ioctl for device)<br>
<br>
Apparently that means I'm trying to communicate with it as though it<br>
were a type writer. How fitting :)<br>
<br>
The commands are all simple strings as docs here:<br>
<br>
    <a href="http://www.mplayerhq.hu/DOCS/tech/slave.txt" target="_blank">http://www.mplayerhq.hu/DOCS/tech/slave.txt</a><br>
<br>
My questions are these: is there anything I need to take care of when<br>
handling sub-processes like this, specifically while writing to stdin of<br>
the process, and with particular regard to -threaded? Does anybody spot<br>
a problem or something I'm overlooking when handling processes like this?<br>
I have been reading the API docs, but found no mention of potential<br>
caveats pertaining to -threaded.<br>
<br>
Thanks!<br>
<br>
k<br>
<br>
<br>
✉ <a href="mailto:k@ioctl.it">k@ioctl.it</a><br>
☎ <a href="tel:%2B49%280%29176%20%2F%2061995110" value="+4917661995110">+49(0)176 / 61995110</a><br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Gregory Collins <<a href="mailto:greg@gregorycollins.net" target="_blank">greg@gregorycollins.net</a>>
</div>