Is this just a problem of spawning too many forkIO resources that never produce a result?<div><br></div><div>I was thinking of trying something like the following in System.Timeout&#39;s place:</div><div><br></div><div><div>
&gt; module Main where</div><div><br></div><div>&gt; import Control.Concurrent.MVar</div><div>&gt; import Control.Concurrent</div><div>&gt; import Data.Maybe</div><div><br></div></div><div><br></div><div><div>&gt; timeout :: Int -&gt; IO a -&gt; IO (Maybe a)</div>
<div>&gt; timeout time action = do</div><div>&gt;   someMVar &lt;- newEmptyMVar   -- MVar is a Maybe</div><div>&gt;   timeoutThread &lt;- forkIO $ nothingIzer time someMVar</div><div>&gt;   forkIO $ actionRunner action someMVar timeoutThread</div>
<div>&gt;   takeMVar someMVar &gt;&gt;= return</div><div>&gt;     where </div><div>&gt;       nothingIzer time mvar = threadDelay time &gt;&gt; putMVar mvar Nothing</div><div>&gt;       actionRunner action mvar timeoutThread = do </div>
<div>&gt;                         res &lt;- action</div><div>&gt;                         killThread timeoutThread</div><div>&gt;                        putMVar mvar $ Just res</div><div><br></div><div>&gt; main :: IO ()</div>
<div>&gt; main = do </div><div>&gt;  res &lt;- timeout (5 * 10 ^ 6) (getLine &gt;&gt;= putStrLn)</div><div>&gt;  case res of</div><div>&gt;     Nothing -&gt; putStrLn &quot;Timeout&quot;</div><div>&gt;     Just x -&gt; putStrLn &quot;Success&quot;</div>
<div><br></div><div><br></div><br><div class="gmail_quote">On Tue, Mar 23, 2010 at 11:31 AM, Roel van Dijk <span dir="ltr">&lt;<a href="mailto:vandijk.roel@gmail.com">vandijk.roel@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I tried a few things. First I added another timeout to main, so the<br>
program kills itself after a few seconds.<br>
<br>
doit :: IO (Maybe ())<br>
doit = timeout 12000000 $ {- yield &gt;&gt; -} return ()<br>
<br>
main :: IO ()<br>
main = do _ &lt;- timeout 5000000 $ forever doit<br>
          return ()<br>
<br>
This program failed to terminate. But when I compiled -with threaded<br>
and added a yield to doit, it worked (kinda). If the timeout in doit<br>
is not too long, like 200 milliseconds, the program has constant space<br>
usage. But when I increased the timeout in doit to 12 seconds I got a<br>
stack overflow.<br>
<br>
I&#39;ll investigate further when I have more time.<br>
<br>
Regards,<br>
<font color="#888888">Roel<br>
</font></blockquote></div><br></div>