Polysemy: Async gotchas
Gautier DI FOLCO March 08, 2023 [Haskell] #haskell #polysemy #design #effects systemsPolysemy comes with many functions and interpreters relying on -threaded
.
However, whenever an issue (such as an Error
) happens:
= do
embed $ putStrLn "Failing"
throw ()
actFailing = do
a <- async failing
embed $ threadDelay 250
embed $ putStrLn "Do stuff"
r <- await a
embed $ putStrLn "Do even more stuff"
embed $ print r
main = do
runM (runError @() $ asyncToIO actFailing) >>= print
failing
The main thread stops:
Failing
Left ()
Do stuff
Be reassured, the nominal case works:
= do
embed $ putStrLn "Working"
return 42
actWorking = do
a <- async working
embed $ threadDelay 250
embed $ putStrLn "Do stuff"
r <- await a
embed $ putStrLn "Do even more stuff"
embed $ print r
main = do
runM (runError @() $ asyncToIO actWorking) >>= print
working
Working
Do stuff
Do even more stuff
Just 42
Right ()
See the full the code here.