Last active
July 13, 2018 18:25
-
-
Save bimmerlabs/10db819f5b5e7b96c078e96046b420d7 to your computer and use it in GitHub Desktop.
Subprocess error: Connection already closed when using a line like "if (defined $self->param('foo') { ... }" and the param is not defined
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
# it turns out a race condition was being caused, that accidentally 'won' every time before - | |
# but the updated Mojolicious made the broken code apparent. This no longer works: | |
get '/' => sub { | |
my $self = shift; | |
$self->render_later; | |
Mojo::IOLoop->subprocess( | |
sub { | |
my $subprocess = shift; | |
my $template; | |
# if you uncomment this line so param('foo) is defined, it works | |
# $self->param(foo => ''); | |
if (defined $self->param('foo')) { | |
say $self->param('foo'); | |
$template = 'foo'; | |
} | |
else { | |
say 'there is no foo'; | |
$template = 'index'; | |
} | |
return ($template); | |
}, | |
sub { | |
my ($subprocess, $err, $template) = @_; | |
say "Subprocess error: $err" and $self->render('index') | |
if $err; | |
$self->render($template); | |
} | |
); | |
}; | |
app->start; | |
__DATA__ | |
@@ foo.html.ep | |
% layout 'default'; | |
% title 'Welcome'; | |
<%= param 'foo' %> | |
@@ index.html.ep | |
% layout 'default'; | |
% title 'Welcome'; | |
There is no foo. | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head><title><%= title %></title></head> | |
<body><%= content %></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
# by using Mojolicious::Plugin::Subprocess, the race condition is avoided. The code is shorter/cleaner too. | |
plugin 'Subprocess'; | |
get '/' => sub { | |
my $self = shift; | |
$self->render_later; | |
$self->subprocess( | |
sub { | |
my $subprocess = shift; | |
my $template; | |
if (defined $self->param('foo')) { | |
say $self->param('foo'); | |
$template = 'foo'; | |
} | |
else { | |
say 'there is no foo'; | |
$template = 'index'; | |
} | |
return ($template); | |
}, | |
sub { | |
my ($subprocess, $template) = @_; | |
$self->render($template); | |
} | |
); | |
}; | |
app->start; | |
__DATA__ | |
@@ foo.html.ep | |
% layout 'default'; | |
% title 'Welcome'; | |
<%= param 'foo' %> | |
@@ index.html.ep | |
% layout 'default'; | |
% title 'Welcome'; | |
There is no foo. | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<head><title><%= title %></title></head> | |
<body><%= content %></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment