Polysemy: Async gotchas
Polysemy comes with many functions and interpreters relying on -threaded
.
However, whenever an issue (such as an Error
) happens:
failing = 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
The main thread stops:
Failing
Left ()
Do stuff
Be reassured, the nominal case works:
working = 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
Do stuff
Do even more stuff
Just 42
Right ()
See the full the code here.