| 1 | use v5.40; | ||
| 2 | no warnings qw[experimental::class recursion]; | ||
| 3 | use feature 'class'; | ||
| 4 | # | ||
| 5 | # Structured concurrency: an enclosed block spawns child fibers that are all guaranteed to | ||
| 6 | # finish before the enclosing nursery returns, and that are all cancelled the moment any | ||
| 7 | # one of them fails. `Acme::Parataxis->nursery( sub ($n) { ... } )` builds one of these, | ||
| 8 | # runs the block in the calling fiber (the parent), joins every child, and rethrows the | ||
| 9 | # first failure as an Acme::Parataxis::Error::Nursery whose ->failures lists every child | ||
| 10 | # error (Principle 4: a plain aggregate error, no Java ExceptionGroup). | ||
| 11 | class Acme::Parataxis::Nursery v0.1.6 { | ||
| 12 | use Acme::Parataxis; | ||
| 13 | use Acme::Parataxis::CancellationToken; | ||
| 14 | use Carp qw[croak]; | ||
| 15 | |||
| 16 | # The token that cancels every enrolled child. Cancelling it from user code (or passing | ||
| 17 | # it to a nested with_timeout) cancels the whole nursery, as if a sibling had failed. | ||
| 18 | field $token : reader : param = Acme::Parataxis::CancellationToken->new; | ||
| 19 | |||
| 20 | # Every enrolled child, in spawn order. Kept alive until the join so no child can be | ||
| 21 | # destroyed (or orphaned) before the nursery has drained it. | ||
| 22 | field @children; | ||
| 23 | |||
| 24 | # Spawn a child fiber enrolled in this nursery. The child parks once at birth instead of | ||
| 25 | # running inline, so a failure in its very first statement surfaces through the scheduler | ||
| 26 | # (where the nursery can observe and aggregate it) rather than being thrown into the | ||
| 27 | # block. Only children created through ->spawn are aggregated: a bare spawn/fiber inside | ||
| 28 | # the block is an independent fiber the block owns itself. | ||
| 29 | method spawn ($code) { | ||
| 30 | croak 'Nursery::spawn() requires a CODE ref' unless ref $code eq 'CODE'; | ||
| 31 | croak 'Nursery::spawn() must be called from inside a scheduled fiber' if Acme::Parataxis->current_fid < 0; | ||
| 32 | my $tok = $token; | ||
| 33 | my $child = Acme::Parataxis->spawn( | ||
| 34 | sub { | ||
| 35 | $tok->register; | ||
| 36 | Acme::Parataxis->yield; # birth park: this run never happens inline into the parent | ||
| 37 | my $rv; | ||
| 38 | my $err; | ||
| 39 | try { $rv = $code->() } catch ($e) { | ||
| 40 | $err = $e | ||
| 41 | } | ||
| 42 | $tok->unregister; | ||
| 43 | die $err if $err; | ||
| 44 | return $rv; | ||
| 45 | } | ||
| 46 | ); | ||
| 47 | $child->on_ready( sub { $self->_observe( $_[0] ) } ); | ||
| 48 | push @children, $child; | ||
| 49 | return $child; | ||
| 50 | } | ||
| 51 | |||
| 52 | # Fires the moment an enrolled child dies. Prompt sibling cancellation: interrupt every | ||
| 53 | # other child still parked, so each throws Error::Cancelled at its next park re-entry and | ||
| 54 | # unwinds; their errors land in the join's failure list. A natural completion is not a | ||
| 55 | # failure and leaves the siblings alone. | ||
| 56 | method _observe ($child) { | ||
| 57 | return unless defined $child->error; | ||
| 58 | $token->cancel; | ||
| 59 | } | ||
| 60 | |||
| 61 | # Await every child until it is done and reap its C coroutine, so nothing is left running | ||
| 62 | # on any exit path and even a still-referenced child shell cannot leak a live fiber. | ||
| 63 | # Returns the observed child errors (natural failures plus the Error::Cancelled unwinds of | ||
| 64 | # the siblings they cancelled). | ||
| 65 | # | ||
| 66 | # If the *parent* is interrupted while parked in a child's await (e.g. an enclosing | ||
| 67 | # with_timeout fired its deadline mid-join), that is not a child failure: the remaining | ||
| 68 | # children are cancelled and drained, and the parent's own error is propagated. | ||
| 69 | method _join () { | ||
| 70 | for my $child (@children) { | ||
| 71 | next if $child->is_done; | ||
| 72 | my $ok = 1; | ||
| 73 | my $e; | ||
| 74 | try { $child->await } catch ($caught) { | ||
| 75 | $ok = 0; | ||
| 76 | $e = $caught | ||
| 77 | } | ||
| 78 | if ($ok) { | ||
| 79 | $child->is_done; | ||
| 80 | next; | ||
| 81 | } | ||
| 82 | |||
| 83 | # If the child finished/died, its error will be aggregated below. | ||
| 84 | next if $child->is_done; | ||
| 85 | |||
| 86 | # The parent was interrupted mid-await: cancel siblings and drain. | ||
| 87 | warn sprintf "PARATAXIS_TRACE t=%.0fms fid=%d join got parent-interrupt error=%s site=%s\n", ( time - $^T ) * 1000, | ||
| 88 | Acme::Parataxis::current_fid, ref( $e || '' ) || "plain:$e", "$e" | ||
| 89 | if $ENV{PARATAXIS_TRACE}; | ||
| 90 | $token->cancel; | ||
| 91 | for my $c (@children) { | ||
| 92 | next if $c->is_done; | ||
| 93 | |||
| 94 | # Deliberately a *different* variable: $e is still the parent-interrupt error that | ||
| 95 | # gets rethrown below, and a drain failure must not overwrite it. | ||
| 96 | try { $c->await } catch ($drain_err) { | ||
| 97 | } | ||
| 98 | $c->is_done; | ||
| 99 | } | ||
| 100 | warn sprintf "PARATAXIS_TRACE t=%.0fms fid=%d join draining done, rethrowing error=%s\n", ( time - $^T ) * 1000, | ||
| 101 | Acme::Parataxis::current_fid, ref( $e || '' ) || "plain:$e" | ||
| 102 | if $ENV{PARATAXIS_TRACE}; | ||
| 103 | die $e; | ||
| 104 | } | ||
| 105 | return grep { defined $_ } map { $_->error } @children; | ||
| 106 | } | ||
| 107 | }; | ||
| 108 | # | ||
| 109 | 1; |