Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed small memory leak in Mojolicious
  • Loading branch information
kraih committed Oct 11, 2011
1 parent dc99804 commit 930c0d9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -20,6 +20,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Fixed on_finish callback to work consistently with the user agent.
- Fixed big memory and file descriptor leak in the TLS code of
Mojo::IOLoop::Server.
- Fixed small memory leak in Mojolicious.
- Fixed small memory leak in Mojo::DOM.
- Fixed small memory leak in Mojo::Message.
- Fixed small formatting bug in Mojo::Headers.
Expand Down
1 change: 1 addition & 0 deletions lib/Mojolicious.pm
Expand Up @@ -171,6 +171,7 @@ sub handler {
my $c =
$self->controller_class->new(app => $self, stash => $stash, tx => $tx);
weaken $c->{app};
weaken $c->{tx};
unless (eval { $self->on_process->($self, $c); 1 }) {
$self->log->fatal("Processing request failed: $@");
$tx->res->code(500);
Expand Down
12 changes: 5 additions & 7 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -145,13 +145,11 @@ sub on_finish {
# "I like being a women.
# Now when I say something stupid, everyone laughs and buys me things."
sub on_message {
my $self = shift;

my ($self, $cb) = @_;
my $tx = $self->tx;
Carp::croak('No WebSocket connection to receive messages from')
unless $tx->is_websocket;
$self->rendered(101);
my $cb = shift;
return $tx->on_message(sub { shift and $self->$cb(@_) });
}

Expand Down Expand Up @@ -755,8 +753,8 @@ Data storage persistent only for the next request, stored in the session.
my $cb = $c->on_finish(sub {...});
Register C<finish> event, which will be emitted when the transaction has been
finished.
Register C<finish> event with transaction, which will be emitted when the
transaction has been finished.
$c->on_finish(sub {
my $c = shift;
Expand All @@ -766,8 +764,8 @@ finished.
my $cb = $c->on_message(sub {...});
Register C<message> event, which will be emitted when new WebSocket messages
arrive.
Register C<message> event with transaction, which will be emitted when new
WebSocket messages arrive.
Note that this method is EXPERIMENTAL and might change without warning!
$c->on_message(sub {
Expand Down
9 changes: 7 additions & 2 deletions t/mojolicious/longpolling_lite_app.t
Expand Up @@ -52,16 +52,21 @@ get '/shortpoll/nolength' => sub {
my $longpoll;
get '/longpoll' => sub {
my $self = shift;
$self->on_finish(sub { $longpoll = 'finished!' });
$self->res->code(200);
$self->res->headers->content_type('text/plain');
$self->write_chunk('hi ');
Mojo::IOLoop->timer(
my $id = Mojo::IOLoop->timer(
'0.5' => sub {
$self->write_chunk('there,', sub { shift->write_chunk(' whats up?') });
shift->timer('0.5' => sub { $self->finish });
}
);
$self->on_finish(
sub {
Mojo::IOLoop->drop($id);
$longpoll = 'finished!';
}
);
};

# GET /longpoll/nolength
Expand Down
27 changes: 26 additions & 1 deletion t/mojolicious/websocket_lite_app.t
Expand Up @@ -11,7 +11,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 78;
use Test::More tests => 87;

use Mojo::ByteStream 'b';
use Mojolicious::Lite;
Expand Down Expand Up @@ -64,6 +64,25 @@ websocket '/bytes' => sub {
);
};

# WebSocket /double
websocket '/double' => sub {
my $self = shift;
$self->on_message(
sub {
my ($self, $message) = @_;
$self->send_message("ONE: $message");
}
);
my $cb;
$cb = $self->on_message(
sub {
my ($self, $message) = @_;
$self->send_message("TWO: $message");
$self->tx->unsubscribe(message => $cb);
}
);
};

# /nested
under '/nested';

Expand Down Expand Up @@ -150,6 +169,12 @@ $t->websocket_ok('/bytes')->send_message_ok([$bytes])->message_is($bytes)
$t->websocket_ok('/bytes')->send_message_ok([$bytes])->message_is($bytes)
->send_message_ok([$bytes])->message_is($bytes)->finish_ok;

# WebSocket /double
$t->websocket_ok('/double')->send_message_ok('hello')
->message_is('ONE: hello')->message_is('TWO: hello')
->send_message_ok('hello')->message_is('ONE: hello')
->send_message_ok('hello')->message_is('ONE: hello')->finish_ok;

# WebSocket /nested
$t->websocket_ok('/nested')->send_message_ok('hello')
->message_is('nested echo: hello')->finish_ok;
Expand Down

0 comments on commit 930c0d9

Please sign in to comment.