12345678910111213141516171819202122232425262728293031323334353637383940414243openStdtypety=[`Domain_mgr]type'at=([>ty]as'a)rmodulePi=structmoduletypeMGR=sigtypetvalrun:t->(cancelled:exnPromise.t->'a)->'avalrun_raw:t->(unit->'a)->'aendtype(_,_,_)Resource.pi+=|Mgr:('t,(moduleMGRwithtypet='t),[>ty])Resource.piletmgr(typet)(moduleX:MGRwithtypet=t)=Resource.handler[H(Mgr,(moduleX))]endletrun_raw(Resource.T(t,ops))fn=letmoduleX=(val(Resource.getopsPi.Mgr))inX.run_rawtfnletrun(Resource.T(t,ops))fn=letmoduleX=(val(Resource.getopsPi.Mgr))inX.runt@@fun~cancelled->(* If the spawning fiber is cancelled, [cancelled] gets set to the exception. *)tryFiber.first(fun()->matchPromise.awaitcancelledwith|Cancel.Cancelledex->raiseex(* To avoid [Cancelled (Cancelled ex))] *)|ex->raiseex(* Shouldn't happen *))fnwithex->matchPromise.peekcancelledwith|Some(Cancel.Cancelledex2ascex)whenex==ex2->(* We unwrapped the exception above to avoid [fn] seeing a double cancelled exception.
But this means that the top-level reported the original exception,
which isn't what we want. *)raisecex|_->raiseex