App-Raider
view release on metacpan or search on metacpan
.claude/skills/perl-io-async-future/SKILL.md view on Meta::CPAN
- A Notifier only knows its loop after `$loop->add($notifier)` â anything that calls `$self->loop` must run after that.
- Child notifiers (`IO::Async::Stream`, `IO::Async::Timer`) attach via `$self->add_child($child)`; they inherit the loop automatically.
---
## Pattern 2 â TCP Connect (the GC trap)
```perl
async sub connect {
my ($self) = @_;
my $stream = IO::Async::Stream->new(
on_read => sub { $self->_on_read(@_) },
on_read_eof => sub { $self->_on_disconnect('read_eof') },
on_read_error => sub { $self->_on_error("read: $_[1]") },
);
$self->{_stream} = $stream;
$self->add_child($stream);
# â ï¸ RETAIN the connect Future. If GC'd, the stream never gets its
# handle and on_read never fires â silent hang.
$self->{_tcp_connect_future} = $stream->connect(
host => $self->{host},
service => $self->{port},
)->on_fail(sub {
my $err = shift;
$self->{_connect_future}->fail("connect: $err")
unless $self->{_connect_future}->is_ready;
});
$self->{_connect_future} = $self->loop->new_future;
return await $self->{_connect_future};
}
```
**Why both futures?** `_tcp_connect_future` resolves when the *socket* is up. `_connect_future` resolves when your *protocol handshake* is done. The handshake completes inside `on_read`, so you need a separate Future to await.
Clean up `_tcp_connect_future` once the handshake succeeds (`delete $self->{_tcp_connect_future}`).
---
## Pattern 3 â Reconnect Without Losing Futures
The reconnect path is where Future-lifetime bugs cluster. Wrong:
```perl
# â BUG: $f is local to the closure, GC'd as soon as _reconnect returns
sub _reconnect {
my $self = shift;
my $f = $self->loop->delay_future(after => 2)->then(sub { $self->connect });
}
```
Right:
```perl
sub _reconnect_attempt {
my ($self) = @_;
return if $self->{_connected};
weaken(my $weak = $self);
$self->{_reconnect_future} = $self->loop
->delay_future(after => $self->{reconnect_wait})
->then(sub {
my $self = $weak or return Future->done;
return Future->done if $self->{_connected};
return $self->connect;
})
->on_done(sub {
my $self = $weak or return;
delete $self->{_reconnect_future};
})
->on_fail(sub {
my $self = $weak or return;
delete $self->{_reconnect_future};
$self->_reconnect_attempt; # try again
});
}
```
**Rules:**
- Store the *whole chain* on the object (`$self->{_reconnect_future}`), not just the leaf.
- `weaken` `$self` inside callbacks â otherwise the chain holds the object alive forever.
- Always `delete $self->{_reconnect_future}` in both `on_done` and `on_fail`, or you'll guard out future reconnects with `return if $self->{_reconnect_future}`.
- On disconnect, **cancel the old `_connect_future`** so its async sub unwinds:
```perl
if (my $f = delete $self->{_connect_future}) {
$f->fail("disconnected: $reason") unless $f->is_ready;
}
```
---
## Pattern 4 â Future Composition
```perl
# Sequential dependency (then = "after this, do that")
$f1->then(sub { do_b(@_) }) # done â done branch
->else(sub { recover(@_) }) # fail â recovery branch
->then(sub { do_c(@_) });
# Parallel, all must succeed (fails fast, cancels siblings)
my $f = Future->needs_all($fa, $fb, $fc);
my @results = await $f;
# Race â first one wins, losers are cancelled
my $f = Future->wait_any($work, $self->loop->delay_future(after => 5)
->then_fail('timeout'));
# Observe without composing (does NOT chain â return value ignored)
$f->on_done(sub { warn "done: @_" });
$f->on_fail(sub { warn "failed: $_[0]" });
```
**`then` vs `on_done`:**
- `then` returns a *new* Future; the callback returns a Future to chain. Use for control flow.
- `on_done` returns the *same* Future; callback return value is discarded. Use for side-effects/observation.
**Protective composition** (Future ⥠0.51): pass extra siblings via `also =>` to keep them alive without making cancellation propagate:
```perl
Future->needs_all($f1, also => $f2, $f3);
```
---
## Pattern 5 â `->retain` for Fire-and-Forget
When you legitimately want to start an op and not wait, but still need it to *finish*:
```perl
$self->_log_async($msg)
->on_fail(sub { warn "log failed: $_[0]" })
->retain;
```
`->retain` parks the Future on an internal global until it completes, then drops it. Without `retain`, the chain has no holder and gets GC'd before it runs. Use this only when you genuinely don't need the result â otherwise hold the Future yourself...
---
## Pattern 6 â Future::AsyncAwait (`async`/`await`)
```perl
use Future::AsyncAwait;
.claude/skills/perl-io-async-future/SKILL.md view on Meta::CPAN
```perl
# Before starting a new attempt, kill stale ones
if (my $f = delete $self->{_connect_future}) {
$f->cancel unless $f->is_ready;
}
```
- `->cancel` is idempotent but only meaningful on a non-ready Future.
- `needs_all` cancels siblings on first failure; `wait_any` cancels losers on first success. You usually don't need to cancel manually inside a composition.
- A cancelled Future is *neither* done nor failed â `is_ready` is true but `result`/`failure` will throw. Check `is_cancelled` if you need to distinguish.
---
## Pattern 9 â Loops & Tests
```perl
use IO::Async::Loop;
my $loop = IO::Async::Loop->new;
$loop->add($client);
# Block-wait on a Future from sync code (test code, main script):
my @result = $client->connect->get;
# Run loop until a Future is ready (older style):
$loop->await($f);
```
**Test pattern:**
```perl
use Test::More;
use IO::Async::Test;
use IO::Async::Loop;
my $loop = IO::Async::Loop->new;
testing_loop($loop); # IO::Async::Test hook
my $client = MyClient->new(...);
$loop->add($client);
my $f = $client->do_thing;
wait_for_future($f); # spins the loop
ok($f->is_done, 'completed');
is_deeply([$f->result], [...]);
```
---
## Decision Guide
| Situation | Use |
|---|---|
| Subclassing IO::Async object | `parent 'IO::Async::Notifier'`, override `configure` |
| Storing async state on $self | `$self->{_foo_future}` â never bare lexicals |
| Sequential dependent ops | `->then` / `async`+`await` |
| Parallel, all required | `Future->needs_all` |
| First-to-finish race | `Future->wait_any` |
| Add timeout to op | `wait_any($op, $loop->delay_future(after=>N)->then_fail('timeout'))` |
| Side-effect observation | `->on_done` / `->on_fail` (don't chain) |
| Truly fire-and-forget | `->retain` (rare â usually you should hold it) |
| Closure capturing $self | `weaken(my $weak = $self)` + null-check inside |
| Cancelling stale attempt | `delete $self->{_f}; $f->cancel unless $f->is_ready` |
| Sync block-wait (tests/scripts) | `$f->get` |
---
## Common Pitfalls (the recurring ones)
- **Local-variable-only Future** â silent GC. Hold it on `$self`.
- **Async sub whose caller drops the Future** â "lost its returning future". Hold the result.
- **Strong `$self` capture in callback chain** â object never destroyed; reconnect loops leak. `weaken` it.
- **Forgetting to `delete` the held Future on completion** â stale guards block future operations.
- **Not chaining `SUPER::configure`** â defaults silently missing, or unknown keys silently accepted.
- **Returning a Future from `async sub` without `await`** â double-wrapped result.
- **Calling `$self->loop` before `$loop->add($self)`** â `loop` is undef.
- **Mixing `then` and `on_done` thinking they're the same** â `on_done` returns the original Future, your "chain" is actually two parallel observers.
- **Cancelling a Future inside its own callback** â undefined; cancel from outside.
- **Using `Future->new` instead of `$loop->new_future`** when you need loop-aware behavior (the loop variant integrates with timeouts and is the recommended form inside Notifier subclasses).
---
## The Mental Model
A Future is a *handle to a result that may not exist yet*. It is also a *Perl SV with a refcount*. Both facts matter equally:
- As a handle, you compose, await, observe.
- As an SV, if nothing holds it, it disappears â and async work it represents either completes into the void or never completes at all.
Every async bug in this codebase has been one of: (a) nobody held the Future, (b) somebody held it too tightly via $self capture, or (c) the Future was held but never deleted, blocking the next operation. The patterns above exist to make all three im...
( run in 1.853 second using v1.01-cache-2.11-cpan-364913b4093 )