Skip to content

Commit

Permalink
fix bug in Mojo::IOLoop where the callback passed to next_tick would …
Browse files Browse the repository at this point in the history
…receive the wrong invocant
  • Loading branch information
kraih committed Jan 26, 2015
1 parent aaf1d54 commit e180c04
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -6,6 +6,8 @@
- Improved Hypnotoad to wait for new workers to be ready before stopping the
old ones during hot deployment.
- Improved commands and log messages to use less punctuation.
- Fixed bug in Mojo::IOLoop where the callback passed to next_tick would
receive the wrong invocant.
- Fixed race condition and memory leak in Mojo::Server::Prefork.

5.74 2015-01-25
Expand Down
10 changes: 8 additions & 2 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -86,8 +86,14 @@ sub delay {
}

sub is_running { _instance(shift)->reactor->is_running }
sub next_tick { _instance(shift)->reactor->next_tick(@_) }
sub one_tick { _instance(shift)->reactor->one_tick }

sub next_tick {
my ($self, $cb) = (_instance(shift), @_);
weaken $self;
return $self->reactor->next_tick(sub { $self->$cb });
}

sub one_tick { _instance(shift)->reactor->one_tick }

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

Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Server/Prefork.pm
Expand Up @@ -155,12 +155,12 @@ sub _spawn {
if $pid;

# Prepare lock file
my $file = $self->{lock_file};
my $file = $self->cleanup(0)->{lock_file};
$self->app->log->error(qq{Can't open lock file "$file": $!})
unless open my $handle, '>', $file;

# Change user/group
$self->cleanup(0)->setuidgid;
$self->setuidgid;

# Accept mutex
weaken $self;
Expand All @@ -185,8 +185,8 @@ sub _spawn {
$loop->unlock(sub { flock $handle, LOCK_UN });

# Heartbeat messages
$loop->next_tick(sub { $self->_heartbeat(0) });
my $cb = sub { $self->_heartbeat(shift->max_connections ? 0 : 1) };
$loop->next_tick($cb);
$loop->recurring($self->heartbeat_interval => $cb);

# Clean worker environment
Expand Down
5 changes: 3 additions & 2 deletions t/mojo/ioloop.t
Expand Up @@ -27,9 +27,10 @@ is ref $loop->reactor, 'MyReactor', 'right class';
my $err;
Mojo::IOLoop->next_tick(
sub {
eval { Mojo::IOLoop->start };
my $loop = shift;
eval { $loop->start };
$err = $@;
Mojo::IOLoop->stop;
$loop->stop;
}
);
Mojo::IOLoop->start;
Expand Down

0 comments on commit e180c04

Please sign in to comment.