From 81a307e03c3b60e5368253aee64bd1e2899beade Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 4 Mar 2021 11:29:28 +0100 Subject: [PATCH] Move exception handling to the right place: inside each turn --- src/actor.ts | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/actor.ts b/src/actor.ts index 2a4ef53..8f2fa06 100644 --- a/src/actor.ts +++ b/src/actor.ts @@ -125,20 +125,10 @@ export class Actor { console.error(`Actor ${this.id} crashed:`, this.exitReason.err); } this.exitHooks.forEach(hook => hook(t)); - queueTask(() => - t.freshen(t => - this.outbound.forEach((peer, h) => t._retract(peer, h)))); - } - - execute(proc: () => void): void { - queueTask(() => { - if (this.exitReason !== null) return; - try { - proc(); - } catch (err) { - Turn.for(this, t => this.terminateWith(t, { ok: false, err })); - } - }); + queueTask(() => Turn.for( + t.actor, + t => this.outbound.forEach((peer, h) => t._retract(peer, h)), + true)); } } @@ -158,11 +148,16 @@ export class Turn { readonly actor: Actor; queues: Map | null = new Map(); - static for(actor: Actor, f: LocalAction): void { + static for(actor: Actor, f: LocalAction, zombieTurn = false): void { + if ((actor.exitReason === null) === zombieTurn) return; const t = new Turn(actor); - f(t); - t.queues!.forEach((q, a) => a.execute(() => q.forEach(f => Turn.for(a, f)))); - t.queues = null; + try { + f(t); + t.queues!.forEach((q, a) => queueTask(() => q.forEach(f => Turn.for(a, f)))); + t.queues = null; + } catch (err) { + Turn.for(actor, t => actor.terminateWith(t, { ok: false, err })); + } } private constructor(actor: Actor) { @@ -180,8 +175,7 @@ export class Turn { newOutbound.set(key, this.actor.outbound.get(key)!); // we trust initialAssertions this.actor.outbound.delete(key); }); - const child = new Actor(newOutbound); - child.execute(() => Turn.for(child, bootProc)); + queueTask(() => Turn.for(new Actor(newOutbound), bootProc)); }); }