user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] qspawn: retry sysread when parsing headers, too
Date: Thu,  4 Jul 2019 10:20:35 +0000	[thread overview]
Message-ID: <20190704102035.31329-1-e@80x24.org> (raw)

We need to ensure the BIN_DETECT (8000 byte) check in
ViewVCS can be handled properly when sending giant
files.  Otherwise, EPOLLET won't notify us, again,
and responses can get stuck.

While we're at it, bump up the read-size up to 4096
bytes so we make fewer trips to the kernel.
---
 lib/PublicInbox/Qspawn.pm | 16 +++++++++++++---
 t/httpd-corner.psgi       | 13 +++++++++++++
 t/httpd-corner.t          | 16 ++++++++++++++--
 3 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/lib/PublicInbox/Qspawn.pm b/lib/PublicInbox/Qspawn.pm
index 8f0b9fe2..fb48585c 100644
--- a/lib/PublicInbox/Qspawn.pm
+++ b/lib/PublicInbox/Qspawn.pm
@@ -195,9 +195,19 @@ sub psgi_return {
 
 	my $buf = '';
 	my $rd_hdr = sub {
-		my $r = sysread($rpipe, $buf, 1024, length($buf));
-		return if !defined($r) && $! == EAGAIN || $! == EINTR;
-		$parse_hdr->($r, \$buf);
+		# we must loop until EAGAIN for EPOLLET in HTTPD/Async.pm
+		# We also need to check EINTR for generic PSGI servers.
+		my $ret;
+		my $n = 0;
+		do {
+			my $r = sysread($rpipe, $buf, 4096, length($buf));
+			return if !defined($r) && $! == EAGAIN || $! == EINTR;
+
+			# $r may be undef, here:
+			$n += $r if $r;
+			$ret = $parse_hdr->($r ? $n : $r, \$buf);
+		} until (defined $ret);
+		$ret;
 	};
 
 	my $wcb = delete $env->{'qspawn.wcb'};
diff --git a/t/httpd-corner.psgi b/t/httpd-corner.psgi
index f8396907..9728aa05 100644
--- a/t/httpd-corner.psgi
+++ b/t/httpd-corner.psgi
@@ -72,6 +72,19 @@ my $app = sub {
 			getline => sub { undef },
 			close => sub { die 'CLOSE FAIL' },
 		);
+	} elsif ($path eq '/async-big') {
+		require PublicInbox::Qspawn;
+		open my $null, '>', '/dev/null' or die;
+		my $rdr = { 2 => fileno($null) };
+		my $cmd = [qw(dd if=/dev/zero count=30 bs=1024k)];
+		my $qsp = PublicInbox::Qspawn->new($cmd, undef, $rdr);
+		return $qsp->psgi_return($env, undef, sub {
+			my ($r, $bref) = @_;
+			# make $rd_hdr retry sysread + $parse_hdr in Qspawn:
+			return until length($$bref) > 8000;
+			close $null;
+			[ 200, [ qw(Content-Type application/octet-stream) ]];
+		});
 	}
 
 	[ $code, $h, $body ]
diff --git a/t/httpd-corner.t b/t/httpd-corner.t
index 5efb9d14..35318b50 100644
--- a/t/httpd-corner.t
+++ b/t/httpd-corner.t
@@ -251,9 +251,10 @@ SKIP: {
 		$have_curl = 1;
 		last;
 	}
-	my $ntest = 2;
+	my $ntest = 4;
 	$have_curl or skip('curl(1) missing', $ntest);
-	my $url = 'http://' . $sock->sockhost . ':' . $sock->sockport . '/sha1';
+	my $base = 'http://' . $sock->sockhost . ':' . $sock->sockport;
+	my $url = "$base/sha1";
 	my ($r, $w);
 	pipe($r, $w) or die "pipe: $!";
 	my $cmd = [qw(curl --tcp-nodelay --no-buffer -T- -HExpect: -sS), $url];
@@ -270,6 +271,17 @@ SKIP: {
 	is($?, 0, 'curl exited successfully');
 	is($err, '', 'no errors from curl');
 	is($out, sha1_hex($str), 'read expected body');
+
+	open my $fh, '-|', qw(curl -sS), "$base/async-big" or die $!;
+	my $n = 0;
+	my $non_zero = 0;
+	while (1) {
+		my $r = sysread($fh, my $buf, 4096) or last;
+		$n += $r;
+		$buf =~ /\A\0+\z/ or $non_zero++;
+	}
+	is($n, 30 * 1024 * 1024, 'got expected output from curl');
+	is($non_zero, 0, 'read all zeros');
 }
 
 {
-- 
EW


                 reply	other threads:[~2019-07-04 10:20 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190704102035.31329-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).