Now I am on my phone, so I can’t dial the exact code for you. But the main idea is to cut out the Warp stream as an async exception. It may seem complicated, but the easiest way to get closer to it is to use the race function from the asynchronous library. Something like that:
toExitVar <- newEmptyMVar
race warp (takeMVar toExitVar)
, , Warp :
putMVar toExitVar ()
, , :
-- stack --resolver lts-9.0 script
{-
module Main where
import Network.Wai
import Network.Wai.Handler.Warp
import Network.HTTP.Types
import Control.Concurrent.Async
import Control.Concurrent.MVar
main :: IO ()
main = do
toDie <- newEmptyMVar
race_ (takeMVar toDie) $ run 3000 $ \req send ->
if pathInfo req == ["die"]
then do
putMVar toDie ()
send $ responseLBS status200 [] "Goodbye!"
else send $ responseLBS status200 [] "Still alive!"