Cancel in-flight HTTP request?

25 views
Skip to first unread message

Felipe Gasper

unread,
Oct 23, 2020, 10:07:02 AM10/23/20
to mojol...@googlegroups.com
Hi all,

Does Mojo::UA offer a control to cancel an in-flight HTTP request? I don’t see one in the docs but wonder if I’m just missing it.

(The promise, of course, can be pre-resolved/rejected, but that doesn’t appear to affect the underlying HTTP request.)

Thank you!

cheers,
-Felipe

Dmitry L.

unread,
Oct 23, 2020, 10:13:42 AM10/23/20
to mojol...@googlegroups.com
https://metacpan.org/pod/Mojo::Transaction#completed
> --
> You received this message because you are subscribed to the Google Groups "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/10E98F2C-174C-4459-AC05-C8FA7F623943%40felipegasper.com.



--
//wbr, Dmitry L.

Felipe Gasper

unread,
Oct 23, 2020, 10:22:17 AM10/23/20
to mojol...@googlegroups.com
Thank you!

When I try:

====
use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;

my $p = $ua->get_p('http://www.google.com');

Mojo::Promise->timer(0.01)->then( sub { print "canceling\n"; $p->completed() } );

Mojo::Promise->timer(2)->wait();
====

… then strace that script, the request continues even after the call to completed().

Am I just “holding it wrong”?

cheers,
-FG
> To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/CAKgzkX24PZUkSOQy1%2BBcx%2BhGkbNsXSPULB5fjDx9kZLb2qOY_w%40mail.gmail.com.

Sebastian Riedel

unread,
Oct 23, 2020, 10:39:55 AM10/23/20
to Mojolicious
You can set a response error on the transaction to cancel it safely. That's what the Mojolicious tests use.

--
sebastian

Dmitry L.

unread,
Oct 23, 2020, 10:42:39 AM10/23/20
to mojol...@googlegroups.com
> my $p = $ua->get_p('http://www.google.com');
>
https://metacpan.org/pod/Mojo::UserAgent#get_p returns a Mojo::Promise object

> Mojo::Promise->timer(0.01)->then( sub { print "canceling\n"; $p->completed() } );
>
https://metacpan.org/pod/Mojo::Promise doesn't have `completed` method
your are calling

You have to build Mojo::Transaction via $ua->build_tx and do ->completed on it



--
//wbr, Dmitry L.

Felipe Gasper

unread,
Oct 23, 2020, 10:45:20 AM10/23/20
to mojol...@googlegroups.com
Ahh ok. Yeah I realized after my initial response to you that that was what was going on there. (And my mojo isn’t new enough to have the nifty new unhandled-rejection warning.)

I’ll look into this (and Sebastian’s as well). Thank you!

-FG

Felipe Gasper

unread,
Oct 26, 2020, 9:41:54 AM10/26/20
to mojol...@googlegroups.com
Follow-up to this … it’s still not canceling.

=====
use v5.26;
use Mojo::UserAgent;

$| = 1;

my $ua = Mojo::UserAgent->new();

my $tx = $ua->build_tx( GET => 'http://google.com' );
$tx->on( connection => sub { $tx->completed(); say "canceled" } );

$ua->start_p($tx)->then( sub { say "finished" } );

Mojo::IOLoop->timer(2, sub { Mojo::IOLoop->stop() });

Mojo::IOLoop->start();
=====

It prints “canceled”, but strace shows that it continues with the request after then. Is there something else I need to do?

Sebastian: Do you recall off-hand which tests set a response error from the client logic? I’ve looked over the ones that seem like they might be related, but I’ve not found it.

Thank you both!

cheers,
-Felipe
Reply all
Reply to author
Forward
0 new messages