mojo icon indicating copy to clipboard operation
mojo copied to clipboard

async await seems to clear @_

Open oetiker opened this issue 3 years ago • 5 comments

  • Mojolicious version: 9.19
  • Perl version: 5.34.0
  • Future::AsyncAwait 0.36+ (0.51)
  • Operating system: Ubuntun 20.04

Steps to reproduce the behavior

#!/usr/bin/env perl
use Mojo::Base -base,-async_await;
use Mojo::Promise;
use Mojo::Util qw(dumper);

async sub wait_one {
    Mojo::Promise->timer(0=>'yes');
}

async sub dump_args {
    print "Before", dumper \@_;
    await wait_one;
    print "After", dumper \@_;
}

Mojo::IOLoop->next_tick(sub {
    dump_args('a');
});

Mojo::IOLoop->start

Expected behavior

Before[
  "a"
]
After[  
  "a"
]

Actual behavior

Before[
  "a"
]
After[]

Maybe related to https://rt.cpan.org/Public/Bug/Display.html?id=130683

oetiker avatar Aug 06 '21 08:08 oetiker

I think any fix would have to come from Future::AsyncAwait cc @leonerd

jberger avatar Aug 06 '21 13:08 jberger

Then again, when combined with the other recent perl advancement signatures (squee!), this should rarely be a problem in practice.

use Mojo::Base -strict, -signatures, -async_await;
use Mojo::Promise;
use Mojo::Util qw(dumper);

async sub dump_args (@args) {
  print "Before", dumper \@args;
  await Mojo::Promise->timer(0);
  print "After", dumper \@args;
}

dump_args('a')->wait;

jberger avatar Aug 06 '21 13:08 jberger

Who would use async/await without signatures? 🤨

kraih avatar Aug 06 '21 14:08 kraih

I found the problem because with perl 5.22 async/await and signatures do not mesh ... and there is this customer who still has some boxed with 5.22 ... it will resolve itself soon ... but the behavior is rather unexpected ...

oetiker avatar Aug 06 '21 15:08 oetiker

You can get the same effect as using signatures by copying from @_ to other variables at the beginning of the sub, as a workaround. As for the bug, you would have to report that to Future::AsyncAwait.

Grinnz avatar Aug 06 '21 15:08 Grinnz