Skip to content

Commit

Permalink
fixed close event bug in Mojo::IOLoop
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 22, 2011
1 parent da1a5f8 commit 1f573b4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -12,6 +12,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Improved documentation.
- Improved CSS of some built-in templates.
- Fixed CSS of built-in exception template.
- Fixed close event bug in Mojo::IOLoop.
- Fixed small redirect_to bug. (judofyr, sri)
- Fixed small attribute selector bug in Mojo::DOM::CSS.
- Fixed small unicode bug in Mojolicious::Plugin::EPRenderer.
Expand Down
17 changes: 12 additions & 5 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -91,14 +91,16 @@ sub connect {
# Events
$stream->on(
close => sub {
my $c = $self->{connections}->{$id};
$c->{finish} = 1;
$c->{close}->($self, $id) if $c->{close};
$self->drop($id);
}
);
weaken $c;
$stream->on(
error => sub {
my $c = delete $self->{connections}->{$id};
my $c = $self->{connections}->{$id};
$c->{finish} = 1;
$c->{error}->($self, $id, pop) if $c->{error};
}
);
Expand Down Expand Up @@ -199,13 +201,15 @@ sub listen {
$c->{read} = $read;
$stream->on(
close => sub {
my $c = delete $self->{connections}->{$id};
my $c = $self->{connections}->{$id};
$c->{finish} = 1;
$c->{close}->($self, $id) if $c->{close};
}
);
$stream->on(
error => sub {
my $c = delete $self->{connections}->{$id};
my $c = $self->{connections}->{$id};
$c->{finish} = 1;
$c->{error}->($self, $id, pop) if $c->{error};
}
);
Expand Down Expand Up @@ -365,7 +369,10 @@ sub _drop {
return $self unless my $watcher = $self->iowatcher;
return $self if $watcher->cancel($id);
if (delete $self->{servers}->{$id}) { delete $self->{listening} }
else { delete((delete($self->{connections}->{$id}) || {})->{stream}) }
else {
delete(($self->{connections}->{$id} || {})->{stream});
delete $self->{connections}->{$id};
}
return $self;
}

Expand Down
17 changes: 16 additions & 1 deletion t/mojo/user_agent.t
Expand Up @@ -10,7 +10,7 @@ BEGIN {
use Test::More;
plan skip_all => 'Windows is too fragile for this test!'
if $^O eq 'MSWin32' || $^O =~ /cygwin/;
plan tests => 73;
plan tests => 76;

use_ok 'Mojo::UserAgent';

Expand All @@ -24,6 +24,15 @@ app->log->level('fatal');
# GET /
get '/' => {text => 'works'};

# GET /timeout
my $timeout = undef;
get '/timeout' => sub {
my $self = shift;
Mojo::IOLoop->singleton->connection_timeout($self->tx->connection => '0.5');
$self->on_finish(sub { $timeout = 1 });
$self->render_later;
};

# Proxy detection
my $ua = Mojo::UserAgent->new;
my $backup = $ENV{HTTP_PROXY} || '';
Expand Down Expand Up @@ -253,6 +262,12 @@ ok $tx->success, 'successful';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'works', 'right content';

# GET / (built-in server times out)
$tx = $ua->get('/timeout');
ok !$tx->success, 'not successful';
is $tx->error, 'Premature connection close.', 'right error';
is $timeout, 1, 'on_finish was called';

# Nested keep alive
my @kept_alive;
$ua->get(
Expand Down

0 comments on commit 1f573b4

Please sign in to comment.