-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't block FRP network if writing to stdin blocks
- Loading branch information
Showing
4 changed files
with
232 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
{-# LANGUAGE ConstraintKinds #-} | ||
module HeadlessHost where | ||
|
||
import Data.Maybe | ||
import Reflex | ||
import Control.Monad.Fix | ||
import Control.Monad.Primitive | ||
import Reflex.Host.Class | ||
import Control.Monad.IO.Class | ||
import Control.Monad.Ref | ||
import Data.IORef | ||
import Data.Dependent.Sum | ||
import Control.Concurrent.Chan (newChan, readChan) | ||
import Control.Monad (forM, forM_) | ||
import Control.Monad.Identity (Identity(..)) | ||
|
||
type HeadlessResult t = Event t () | ||
|
||
type MonadHeadlessApp t m = | ||
( Reflex t | ||
, MonadHold t m | ||
, MonadFix m | ||
, PrimMonad (HostFrame t) | ||
, ReflexHost t | ||
, MonadIO (HostFrame t) | ||
, Ref m ~ IORef | ||
, Ref (HostFrame t) ~ IORef | ||
, MonadRef (HostFrame t) | ||
, NotReady t m | ||
, TriggerEvent t m | ||
, PostBuild t m | ||
, PerformEvent t m | ||
, MonadIO m | ||
, MonadIO (Performable m) | ||
, Adjustable t m | ||
) | ||
|
||
runHeadlessApp | ||
:: (forall t m. MonadHeadlessApp t m => m (HeadlessResult t)) | ||
-> IO () | ||
runHeadlessApp guest = | ||
(runSpiderHost :: SpiderHost Global a -> IO a) $ do | ||
(postBuild, postBuildTriggerRef) <- newEventWithTriggerRef | ||
events <- liftIO newChan | ||
(result, fc@(FireCommand fire)) <- do | ||
hostPerformEventT $ | ||
flip runPostBuildT postBuild $ | ||
flip runTriggerEventT events $ | ||
guest | ||
mPostBuildTrigger <- readRef postBuildTriggerRef | ||
forM_ mPostBuildTrigger $ \postBuildTrigger -> | ||
fire [postBuildTrigger :=> Identity ()] $ return () | ||
shutdown <- subscribeEvent result | ||
fix $ \loop -> do | ||
ers <- liftIO $ readChan events | ||
stop <- fireEventTriggerRefs fc ers $ readEvent shutdown >>= \case | ||
Nothing -> return False | ||
Just _ -> return True | ||
if or stop | ||
then return () | ||
else loop | ||
where | ||
-- TODO Some part of this is probably general enough to belong in reflex | ||
-- | Use the given 'FireCommand' to fire events that have subscribers | ||
-- and call the callback for the 'TriggerInvocation' of each. | ||
fireEventTriggerRefs | ||
:: (Monad (ReadPhase m), MonadIO m) | ||
=> FireCommand t m | ||
-> [DSum (EventTriggerRef t) TriggerInvocation] | ||
-> ReadPhase m a | ||
-> m [a] | ||
fireEventTriggerRefs (FireCommand fire) ers rcb = do | ||
mes <- liftIO $ | ||
forM ers $ \(EventTriggerRef er :=> TriggerInvocation a _) -> do | ||
me <- readIORef er | ||
return $ fmap (\e -> e :=> Identity a) me | ||
a <- fire (catMaybes mes) rcb | ||
liftIO $ forM_ ers $ \(_ :=> TriggerInvocation _ cb) -> cb | ||
return a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
{-# language ScopedTypeVariables #-} | ||
|
||
module Main where | ||
|
||
import Control.Concurrent (MVar, newEmptyMVar, takeMVar, tryPutMVar) | ||
import Control.Concurrent.Async (race) | ||
import Control.Exception (finally) | ||
import Control.Monad (void) | ||
import Control.Monad.IO.Class (liftIO) | ||
import Data.ByteString (ByteString) | ||
import Data.IORef (newIORef, writeIORef, readIORef) | ||
import Reflex | ||
import System.Timeout (timeout) | ||
import qualified Data.ByteString.Char8 as BS | ||
import qualified System.Process as P | ||
|
||
import HeadlessHost | ||
import Reflex.Process | ||
import Test.Hspec | ||
|
||
main :: IO () | ||
main = hspec $ do | ||
describe "reflex-process" $ do | ||
it "isn't blocked by a downstream non-blocking process" $ do | ||
timeoutWrapperAsync (checkFRPBlocking goodProcess) `shouldReturn` Right (Just Exit) | ||
it "isn't blocked by a downstream blocking process" $ do | ||
timeoutWrapperAsync (checkFRPBlocking blockingProcess) `shouldReturn` Right (Just Exit) | ||
|
||
-------------------------------CheckFrpBlocking--------------------------------- | ||
goodProcess, blockingProcess :: P.CreateProcess | ||
goodProcess = P.proc "cat" [] | ||
blockingProcess = P.proc "sleep" ["infinity"] | ||
|
||
-- This datatype signals that the FRP network was able to exit on its own. | ||
data Exit = Exit deriving (Show, Eq) | ||
|
||
-- This creates the MVar with which the FRP network sends the exit signals, and | ||
-- checks if a response from the FRP networks comes back in the allotted time. | ||
timeoutWrapperAsync :: (MVar Exit -> IO ()) -> IO (Either () (Maybe Exit)) | ||
timeoutWrapperAsync wrapped = do | ||
exitCommMVar :: MVar Exit <- newEmptyMVar | ||
race (wrapped exitCommMVar) (timeout (3*1000000) (takeMVar exitCommMVar)) | ||
|
||
-- The frp network spawns, starts a timer, and tries to send some long input to | ||
-- the created process. If the underlying process blocks and is able to block | ||
-- the FRP network, the first tick of the timer will never happen, and, the Exit | ||
-- signal will never be put in the MVar. | ||
checkFRPBlocking :: P.CreateProcess -> MVar Exit -> IO () | ||
checkFRPBlocking downstreamProcess exitMVar = do | ||
spawnedProcess <- newIORef Nothing | ||
|
||
let | ||
createProcessWithTermination cp = do | ||
procData <- P.createProcess cp | ||
writeIORef spawnedProcess (Just procData) | ||
pure procData | ||
|
||
finally | ||
(runHeadlessApp $ do | ||
timer <- tickLossyFromPostBuildTime 1 | ||
void $ performEvent $ (liftIO $ tryPutMVar exitMVar Exit) <$ timer | ||
|
||
(ev, evTrigger :: ByteString -> IO ()) <- newTriggerEvent | ||
processOutput <- createProcess downstreamProcess (ProcessConfig ev never createProcessWithTermination) | ||
liftIO $ evTrigger $ veryLongByteString 'a' | ||
liftIO $ evTrigger $ veryLongByteString 'b' | ||
liftIO $ evTrigger $ veryLongByteString 'c' | ||
|
||
void $ performEvent $ liftIO . BS.putStrLn <$> (_process_stdout processOutput) | ||
pure never) | ||
|
||
(readIORef spawnedProcess >>= mapM_ P.cleanupProcess) | ||
|
||
-- It's important to try this with long bytestrings to be sure that they're not | ||
-- put in an operative system inter-process buffer. | ||
veryLongByteString :: Char -> ByteString | ||
veryLongByteString c = BS.replicate 100000 c | ||
-------------------------------------------------------------------------------- |