]> Sergey Matveev's repositories - public-inbox.git/commitdiff
git-http-backend: fix aborts for generic PSGI clone
authorEric Wong <e@80x24.org>
Fri, 27 May 2016 05:59:16 +0000 (05:59 +0000)
committerEric Wong <e@80x24.org>
Fri, 27 May 2016 06:02:33 +0000 (06:02 +0000)
We need to avoid circular references in the generic PSGI layer,
do it by abusing DESTROY.

lib/PublicInbox/GetlineBody.pm [new file with mode: 0644]
lib/PublicInbox/GitHTTPBackend.pm

diff --git a/lib/PublicInbox/GetlineBody.pm b/lib/PublicInbox/GetlineBody.pm
new file mode 100644 (file)
index 0000000..4f8765b
--- /dev/null
@@ -0,0 +1,31 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# Wrap a pipe or file for PSGI streaming response bodies and calls the
+# end callback when the object goes out-of-scope.
+# This depends on rpipe being _blocking_ on getline.
+package PublicInbox::GetlineBody;
+use strict;
+use warnings;
+
+sub new {
+       my ($class, $rpipe, $end, $buf) = @_;
+       bless { rpipe => $rpipe, end => $end, buf => $buf }, $class;
+}
+
+sub DESTROY { $_[0]->close }
+
+sub getline {
+       my ($self) = @_;
+       my $buf = delete $self->{buf};
+       defined $buf ? $buf : $self->{rpipe}->getline;
+}
+
+sub close {
+       my ($self) = @_;
+       delete $self->{rpipe};
+       my $end = delete $self->{end} or return;
+       $end->();
+}
+
+1;
index 9464cb499a3f0c4744b8b33eec1559e415da4887..fd7afbcc3e3c29ff1ba40a642262dac168d4e068 100644 (file)
@@ -186,7 +186,7 @@ sub serve_smart {
        my $x = PublicInbox::Qspawn->new([qw(git http-backend)], \%env, \%rdr);
        my ($fh, $rpipe);
        my $end = sub {
-               $rpipe = undef;
+               close $rpipe if $rpipe && !$fh; # generic PSGI
                if (my $err = $x->finish) {
                        err($env, "git http-backend ($git_dir): $err");
                        drop_client($env);
@@ -201,7 +201,7 @@ sub serve_smart {
                my $r = sysread($rpipe, $buf, 1024, length($buf));
                return if !defined($r) && ($!{EINTR} || $!{EAGAIN});
                return r(500, 'http-backend error') unless $r;
-               $r = parse_cgi_headers(\$buf) or return;
+               $r = parse_cgi_headers(\$buf) or return; # incomplete headers
                $r->[0] == 403 ? serve_dumb($cgi, $git, $path) : $r;
        };
        my $res;
@@ -220,13 +220,8 @@ sub serve_smart {
                }
 
                # for synchronous PSGI servers
-               $r->[2] = Plack::Util::inline_object(
-                       close => $end,
-                       getline => sub {
-                               my $ret = $buf;
-                               $buf = undef;
-                               defined $ret ? $ret : $rpipe->getline;
-                       });
+               require PublicInbox::GetlineBody;
+               $r->[2] = PublicInbox::GetlineBody->new($rpipe, $end, $buf);
                $res->($r);
        };
        sub {