Skip to content

Commit

Permalink
improve one_tick method in Mojo::IOLoop to protect from recursion, si…
Browse files Browse the repository at this point in the history
…milar to the start method (closes #1015)
  • Loading branch information
kraih committed Nov 7, 2016
1 parent 49dd3e7 commit 0c79e9a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
4 changes: 3 additions & 1 deletion Changes
@@ -1,5 +1,7 @@

7.11 2016-11-02
7.11 2016-11-07
- Improved one_tick method in Mojo::IOLoop to protect from recursion, similar
to the start method.

7.10 2016-11-01
- Added getopt function to Mojo::Util.
Expand Down
9 changes: 6 additions & 3 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -81,7 +81,11 @@ sub next_tick {
return $self->reactor->next_tick(sub { $self->$cb });
}

sub one_tick { _instance(shift)->reactor->one_tick }
sub one_tick {
my $self = _instance(shift);
croak 'Mojo::IOLoop already running' if $self->is_running;
$self->reactor->one_tick;
}

sub recurring { shift->_timer(recurring => @_) }

Expand Down Expand Up @@ -482,8 +486,7 @@ callbacks that have been registered with this method, always returns C<undef>.
Mojo::IOLoop->one_tick;
$loop->one_tick;
Run event loop until an event occurs. Note that this method can recurse back
into the reactor, so you need to be careful.
Run event loop until an event occurs.
# Don't block longer than 0.5 seconds
my $id = Mojo::IOLoop->timer(0.5 => sub {});
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Reactor/EV.pm
Expand Up @@ -144,8 +144,7 @@ Construct a new L<Mojo::Reactor::EV> object.
$reactor->one_tick;
Run reactor until an event occurs or no events are being watched anymore. Note
that this method can recurse back into the reactor, so you need to be careful.
Run reactor until an event occurs or no events are being watched anymore.
# Don't block longer than 0.5 seconds
my $id = $reactor->timer(0.5 => sub {});
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Reactor/Poll.pm
Expand Up @@ -232,8 +232,7 @@ callbacks that have been registered with this method, always returns C<undef>.
$reactor->one_tick;
Run reactor until an event occurs or no events are being watched anymore. Note
that this method can recurse back into the reactor, so you need to be careful.
Run reactor until an event occurs or no events are being watched anymore.
# Don't block longer than 0.5 seconds
my $id = $reactor->timer(0.5 => sub {});
Expand Down
12 changes: 12 additions & 0 deletions t/mojo/ioloop.t
Expand Up @@ -42,6 +42,18 @@ Mojo::IOLoop->next_tick(
Mojo::IOLoop->start;
like $err, qr/^Mojo::IOLoop already running/, 'right error';

# Double one_tick
$err = undef;
Mojo::IOLoop->next_tick(
sub {
my $loop = shift;
eval { $loop->one_tick };
$err = $@;
}
);
Mojo::IOLoop->one_tick;
like $err, qr/^Mojo::IOLoop already running/, 'right error';

# Basic functionality
my ($ticks, $timer, $hirestimer);
my $id = $loop->recurring(0 => sub { $ticks++ });
Expand Down

0 comments on commit 0c79e9a

Please sign in to comment.