Skip to content

Commit

Permalink
fixed a few memory leaks in Mojo::IOLoop::Delay
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 27, 2014
1 parent c7518f0 commit b8e21f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/Mojo/IOLoop/Delay.pm
Expand Up @@ -20,11 +20,15 @@ sub data { shift->Mojo::_dict(data => @_) }

sub pass { $_[0]->begin->(@_) }

sub remaining { $REMAINING{shift()} //= [] }
sub remaining {
my $self = shift;
return $REMAINING{$self} //= [] unless @_;
$REMAINING{$self} = shift;
return $self;
}

sub steps {
my $self = shift;
@{$self->remaining} = @_;
my $self = shift->remaining([@_]);
$self->ioloop->next_tick($self->begin);
return $self;
}
Expand Down Expand Up @@ -54,10 +58,10 @@ sub _step {
$self->{counter} = 0;
if (my $cb = shift @{$self->remaining}) {
eval { $self->$cb(@args); 1 }
or (++$self->{fail} and return $self->emit(error => $@));
or (++$self->{fail} and return $self->remaining([])->emit(error => $@));
}

return $self->emit(finish => @args) unless $self->{counter};
return $self->remaining([])->emit(finish => @args) unless $self->{counter};
$self->ioloop->next_tick($self->begin) unless $self->{pending};
return $self;
}
Expand Down Expand Up @@ -214,6 +218,7 @@ values to the next step.
=head2 remaining
my $remaining = $delay->remaining;
$delay = $delay->remaining([]);
Remaining L</"steps"> in chain, stored outside the object to protect from
circular references.
Expand Down
5 changes: 5 additions & 0 deletions t/mojo/delay.t
Expand Up @@ -129,6 +129,7 @@ $delay->steps(
sub { push @results, 'fail' }
);
is_deeply [$delay->wait], [23], 'right return values';
is_deeply $delay->remaining, [], 'no remaining steps';
is_deeply \@results, [[23], [23]], 'right results';

# Finish steps with event
Expand Down Expand Up @@ -221,6 +222,7 @@ $delay->on(error => sub { $failed = pop });
$delay->on(finish => sub { $finished++ });
$delay->steps(sub { die 'First step!' }, sub { $result = 'failed' });
is_deeply [$delay->wait], [], 'no return values';
is_deeply $delay->remaining, [], 'no remaining steps';
like $failed, qr/^First step!/, 'right error';
ok !$finished, 'finish event has not been emitted';
ok !$result, 'no result';
Expand All @@ -233,6 +235,7 @@ $delay->on(finish => sub { $finished++ });
$delay->steps(sub { Mojo::IOLoop->next_tick(shift->begin) },
sub { die 'Last step!' });
is scalar $delay->wait, undef, 'no return value';
is_deeply $delay->remaining, [], 'no remaining steps';
like $failed, qr/^Last step!/, 'right error';
ok !$finished, 'finish event has not been emitted';

Expand All @@ -250,6 +253,7 @@ $delay->steps(
sub { $result = 'failed' }
);
$delay->wait;
is_deeply $delay->remaining, [], 'no remaining steps';
like $failed, qr/^Second step!/, 'right error';
ok !$finished, 'finish event has not been emitted';
ok !$result, 'no result';
Expand All @@ -270,6 +274,7 @@ $delay->steps(
sub { $result = 'failed' }
);
Mojo::IOLoop->start;
is_deeply $delay->remaining, [], 'no remaining steps';
like $failed, qr/^Second step!/, 'right error';
ok !$finished, 'finish event has not been emitted';
ok !$result, 'no result';
Expand Down

0 comments on commit b8e21f3

Please sign in to comment.