문제

I'm creating a small program to use with an irc bot that should take a string and then evaluate the string. For this I'm using the hint package, which work very well for my needs. The problem that I now have is that I want to be able to prevent evaluation of expressions that take a vary long to calculate e.g. 2^1000000000.

I tried using the System.Timeout package like this:

import Data.Maybe 
import Language.Haskell.Interpreter
import System.Timeout
import System.Environment (getArgs)

main :: IO()
main = do
  r <- timeout 500000 $ runInterpreter $ hEval arg
  case r of
    Nothing -> putStrLn "Timed out!"
    Just x ->
      case x of
        Left err -> putStrLn (show err)
        Right a  -> putStrLn a

hEval e = do
  setImportsQ [("Prelude", Nothing),("Data.List",Nothing)]
  a <- eval e
  return $ take 200 a  

But it's not working, the timeout does not fire unless I put in such a short time that nothing can be evaluated. I read on the page for the Timeout package that it could have problems with some modules and have to let theme finish but my understanding is not good enough to know if Hint is such a module.

So any help on this would be appreciated, even if it's just to tell me that this isn't going to work.

도움이 되었습니까?

해결책

GHC threads are cooperative. They can only yield or be terminated by asynchronous exceptions when they perform a memory allocation. This normally works fine, but someone malicious can write a tight loop that runs for a significant time without allocating.

The mueval package was created to deal with things like this. It's implemented in terms of hint, but with a lot of extra safety added in various ways.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top