Skip to content

Commit

Permalink
fixed bug where DATA sections sometimes got corrupted after forking, …
Browse files Browse the repository at this point in the history
…which caused applications to fail randomly
  • Loading branch information
kraih committed Aug 17, 2014
1 parent fe95f1c commit 54f0926
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -3,6 +3,8 @@
- Improved Mojolicious::Static to only handle GET and HEAD requests.
- Improved Mojo::URL performance.
- Improved url_for performance slightly.
- Fixed bug where DATA sections sometimes got corrupted after forking, which
caused applications to fail randomly.
- Fixed Mojo::IOLoop::Client to use a timeout for every connection.

5.29 2014-08-16
Expand Down
11 changes: 11 additions & 0 deletions examples/hello-template.pl
@@ -0,0 +1,11 @@
use FindBin;
use lib "$FindBin::Bin/../lib";
use Mojolicious::Lite;

get '/hello';

app->start;
__DATA__
@@ hello.html.ep
Hello World!
6 changes: 5 additions & 1 deletion lib/Mojolicious.pm
Expand Up @@ -188,7 +188,11 @@ sub plugin {
$self->plugins->register_plugin(shift, $self, @_);
}

sub start { shift->commands->run(@_ ? @_ : @ARGV) }
sub start {
my $self = shift;
$_->_warmup for $self->static, $self->renderer;
return $self->commands->run(@_ ? @_ : @ARGV);
}

sub startup { }

Expand Down
41 changes: 21 additions & 20 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -29,7 +29,6 @@ $HOME->parse(
$HOME->parse($HOME->mojo_lib_dir)->rel_dir('Mojolicious/templates'));
my %TEMPLATES = map { $_ => slurp $HOME->rel_file($_) } @{$HOME->list_files};

# For templates from DATA sections
my $LOADER = Mojo::Loader->new;

sub DESTROY { Mojo::Util::_teardown($_) for @{shift->{namespaces}} }
Expand Down Expand Up @@ -58,13 +57,7 @@ sub add_helper { shift->_add(helpers => @_) }
sub get_data_template {
my ($self, $options) = @_;

# Index DATA templates
unless ($self->{index}) {
my $index = $self->{index} = {};
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for keys %{$LOADER->data($class)};
}
}
$self->_warmup unless $self->{index};

# Find template
my $template = $self->template_name($options);
Expand Down Expand Up @@ -230,18 +223,7 @@ sub _extends {

sub _handlers {
my ($self, $file) = @_;

unless ($self->{templates}) {

# Templates
s/\.(\w+)$// and push @{$self->{templates}{$_}}, $1
for map { sort @{Mojo::Home->new($_)->list_files} } @{$self->paths};

# DATA templates
s/\.(\w+)$// and push @{$self->{templates}{$_}}, $1
for map { sort keys %{$LOADER->data($_)} } @{$self->classes};
}

$self->_warmup unless $self->{templates};
return $self->{templates}{$file};
}

Expand All @@ -260,6 +242,25 @@ sub _render_template {
return undef;
}

sub _warmup {
my $self = shift;

my ($index, $templates) = @$self{qw(index templates)} = ({}, {});

# Classes
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for keys %{$LOADER->data($class)};
}

# Templates
s/\.(\w+)$// and push @{$templates->{$_}}, $1
for map { sort @{Mojo::Home->new($_)->list_files} } @{$self->paths};

# DATA templates
s/\.(\w+)$// and push @{$templates->{$_}}, $1
for map { sort keys %{$LOADER->data($_)} } @{$self->classes};
}

1;

=encoding utf8
Expand Down
17 changes: 9 additions & 8 deletions lib/Mojolicious/Static.pm
Expand Up @@ -19,7 +19,6 @@ my $MTIME = time;
my $HOME = Mojo::Home->new;
my $PUBLIC = $HOME->parse($HOME->mojo_lib_dir)->rel_dir('Mojolicious/public');

# For files from DATA sections
my $LOADER = Mojo::Loader->new;

sub dispatch {
Expand Down Expand Up @@ -125,13 +124,7 @@ sub _get_data_file {
# Protect templates
return undef if $rel =~ /\.\w+\.\w+$/;

# Index DATA files
unless ($self->{index}) {
my $index = $self->{index} = {};
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for keys %{$LOADER->data($class)};
}
}
$self->_warmup unless $self->{index};

# Find file
return undef
Expand All @@ -145,6 +138,14 @@ sub _get_file {
return -f $path && -r $path ? Mojo::Asset::File->new(path => $path) : undef;
}

sub _warmup {
my $self = shift;
my $index = $self->{index} = {};
for my $class (reverse @{$self->classes}) {
$index->{$_} = $class for keys %{$LOADER->data($class)};
}
}

1;

=encoding utf8
Expand Down

0 comments on commit 54f0926

Please sign in to comment.