[Haskell-cafe] Help with async library and killing child processes

Michael Hull mikehulluk at gmail.com
Tue Mar 17 20:50:42 UTC 2020

I am currently learning Haskell, and really enjoying it, thankyou to the
community for this well considered language.
I have started using the async library, and I can't quite figure out some
of the documentation, and I wonder if someone could help.


Makes the comment that: "This is a useful variant of async
ensures an Async is never left running unintentionally." However, I can't
see how I would use this.

To make the example more concrete, I have a function that looks like this:

runNodeHwBridgeUdp :: NodeHwInterface -> NodeHwUdpConfig-> IO()
runNodeHwBridgeUdp (NodeHwInterface l0up l0down) config = do
  putStrLn "Starting Node bridge"

  -- Launch all the subprocesses:
  t0 <- async $  (runMsgUpReader hwBridgeSharedData l0up)
  t1 <- async $  (evalStateT (runUDPWriter hwBridgeSharedData)
(initialUdpDispatchData timeUs) )
  t2 <- async $  (runUDPReader hwBridgeSharedData l0down)

  putStrLn "Created child processes. Waiting...."
  (a, is_exception) <- waitAnyCatchCancel [t0, t1, t2]

  case is_exception of
    Right _ -> do
            error "SHOULD NOT GET HERE"
    Left exception -> do
            case (fromException exception) of
              Just (ex :: HwDestroyException) -> do
                putStrLn $ "BRIDGE ENDING: >>>> GOOD: SHUTDOWN: " ++ show
              Nothing -> do
                  throwIO exception
  return ()

This process launches 3 child processes, and then waits on them. I launch
this function as an async-process, and it would be really helpful if when I
"cancel"ed it, it would also also cancelled the children, but I don't
understand how I would structure that...

  t0 <- withAsync $  (runMsgUpReader hwBridgeSharedData l0up) $ \async ->
  t1 <- withAsync $  (evalStateT (runUDPWriter hwBridgeSharedData)
(initialUdpDispatchData timeUs) ) $ \async -> do
  t2 <- async $  (runUDPReader hwBridgeSharedData l0down) $ \async -> do

I feel like I must be missing something here.
I'd appreciate any input; many thanks for your time,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200317/5e67240c/attachment.html>

More information about the Haskell-Cafe mailing list