.net - PostAndAsyncReply deadlock on exception -
given 2 agents, communicates sending messages:
agent 1 sends
postandasyncreply
agent 2, waiting result.agent 2 starts processing message, throws exception.
now agent 1 deadlocked, since no reply ever sent agent 1. see it, there 2 ways handle such scenario:
i set
timeout
postandasyncreply
, since operation long running, time takes execute differs.wrap code in agent 2 in
try..catch
, , reply value indicates error. however, mean code handles messages replychannel, needs wrapped insidetry..catch
, seems.. cumbersome.
how 1 go around handling such scenario?
you have higher-order function alleviates try...catch
:
let replyorfail (chan: asyncreplychannel<option<'t>>) (action: async<'t>) : async<unit> = async { try let! reply = action return chan.reply (some reply) _ -> return chan.reply none } let agent2 = (* declare mailbox... *) (* matching received message: *) | messagefromagent1 chan -> do! replyorfail chan <| async { return! longcomputationthatmightthrow() } (* ... *)
Comments
Post a Comment