From 577490701aad21a16b5bf6c9badba6376bc4f083 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Thu, 26 Oct 2023 13:13:03 +0100 Subject: [PATCH] Fix runActor exit --- src/syndicate.nim | 3 +-- src/syndicate/actors.nim | 18 ++++++++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/syndicate.nim b/src/syndicate.nim index ce13691..595028d 100644 --- a/src/syndicate.nim +++ b/src/syndicate.nim @@ -192,9 +192,8 @@ type DeprecatedBootProc = proc (ds: Cap; turn: var Turn) {.gcsafe.} proc runActor*(name: string; bootProc: BootProc) = ## Run an `Actor` to completion. let actor = bootDataspace(name, bootProc) - while not actor.future.finished: + while actor.running: waitFor sleepAsync(500) - read(actor.future) proc runActor*(name: string; bootProc: DeprecatedBootProc) {.deprecated.} = ## Run an `Actor` to completion. diff --git a/src/syndicate/actors.nim b/src/syndicate/actors.nim index d8a3abb..7ae868f 100644 --- a/src/syndicate/actors.nim +++ b/src/syndicate/actors.nim @@ -54,7 +54,6 @@ type OutboundTable = Table[Handle, OutboundAssertion] Actor* = ref object - future: Future[void] name: string handleAllocator: ref Handle # a fresh actor gets a new ref Handle and @@ -63,7 +62,7 @@ type exitReason: ref Exception exitHooks: seq[TurnAction] id: ActorId - exiting: bool + exiting, exited: bool when tracing: turnIdAllocator: ref TurnId traceStream: FileStream @@ -440,7 +439,6 @@ proc newActor(name: string; handleAlloc: ref Handle): Actor = handleAllocator: handleAlloc, ) result.root = newFacet(result, nil) - result.future = newFuture[void]($result) when tracing: var act = ActorActivation[void](orKind: ActorActivationKind.start) act.start.actorName = Name[void](orKind: NameKind.named) @@ -485,21 +483,18 @@ proc atExit*(actor; action) = actor.exitHooks.add action proc terminate(actor; turn; reason: ref Exception) = if not actor.exiting: + actor.exiting = true + actor.exitReason = reason when tracing: var act = ActorActivation[void](orKind: ActorActivationKind.stop) if not reason.isNil: act.stop.status = ExitStatus(orKind: ExitStatusKind.Error) act.stop.status.error.message = reason.msg trace(actor, act) - actor.exiting = true - actor.exitReason = reason for hook in actor.exitHooks: hook(turn) proc finish(turn: var Turn) = actor.root.terminate(turn, reason.isNil) - if actor.exitReason.isNil: - actor.future.complete() - else: - actor.future.fail actor.exitReason + actor.exited = true callSoon do (): run(actor.root, finish, true) @@ -603,4 +598,7 @@ proc newCap*(turn; e: Entity): Cap = proc sync*(turn, refer: Cap, cb: proc(t: Turn) {.gcsafe.}) = raiseAssert "not implemented" -proc future*(actor): Future[void] = actor.future +proc running*(actor): bool = + result = not actor.exited + if not (result or actor.exitReason.isNil): + raise actor.exitReason