Permalink
Browse files

Merge pull request #238 from rwstauner/wrapcgi-stdin-hang

Close wrapped cgi's STDIN so the process won't hang
  • Loading branch information...
2 parents 3a409a6 + 2667cdc commit b695d4bab10de4d5502de9e7fac81739398da7e0 @miyagawa miyagawa committed Oct 3, 2011
Showing with 43 additions and 0 deletions.
  1. +2 −0 lib/Plack/App/WrapCGI.pm
  2. +41 −0 t/Plack-Middleware/wrapcgi_exec.t
View
2 lib/Plack/App/WrapCGI.pm
@@ -54,6 +54,8 @@ sub prepare_app {
my $fh = $env->{'psgi.input'};
<$fh>;
});
+ # close STDIN so child will stop waiting
+ close $stdinw;
my $res = '';
while (waitpid($pid, WNOHANG) <= 0) {
View
41 t/Plack-Middleware/wrapcgi_exec.t
@@ -60,5 +60,46 @@ print \$q->header, "Hello " x 10000;
undef $tmp;
}
+# test that wrapped cgi doesn't wait indefinitely for STDIN
+{
+ my $tmp = File::Temp->new(CLEANUP => 1);
+ print $tmp <<"...";
+#!$^X
+print "Content-type: text/plain\\n\\nYou said: ";
+local \$/;
+print <STDIN>;
+...
+ close $tmp;
+
+ chmod(oct("0700"), $tmp->filename) or die "Cannot chmod";
+
+ my $app_exec = Plack::App::WrapCGI->new(script => "$tmp", execute => 1)->to_app;
+ test_psgi app => $app_exec, client => sub {
+ my $cb = shift;
+
+ eval {
+ # without the fix $res->content seems to be "alarm\n" which still fails
+ local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
+ alarm(10);
+ my $res = $cb->(GET "http://localhost/?name=foo");
+ alarm(0);
+ is $res->code, 200;
+ is $res->content, "You said: ";
+
+ alarm(10);
+ $res = $cb->(POST "http://localhost/", Content => "doing things\nthe hard way");
+ alarm(0);
+ is $res->code, 200;
+ is $res->content, "You said: doing things\nthe hard way";
+ };
+ if ( $@ ) {
+ die unless $@ eq "alarm\n"; # propagate unexpected errors
+ ok 0, "request timed out waiting for STDIN";
+ }
+ };
+
+ undef $tmp;
+};
+
done_testing;

0 comments on commit b695d4b

Please sign in to comment.