From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 385BC1FBCC for ; Wed, 10 Jun 2020 07:06:25 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 32/82] imap: fix pipelining with async git Date: Wed, 10 Jun 2020 07:04:29 +0000 Message-Id: <20200610070519.18252-33-e@yhbt.net> In-Reply-To: <20200610070519.18252-1-e@yhbt.net> References: <20200610070519.18252-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Since IMAP yields control to GitAsyncCat, IMAP->event_step may be invoked with {long_cb} still active. We must be sure to bail out of IMAP->event_step if that happens and continue to let GitAsyncCat drive IMAP. This also improves fairness by never processing more than one request per ->event_step. --- lib/PublicInbox/IMAP.pm | 37 ++++++++++++++++------------------ lib/PublicInbox/IMAPdeflate.pm | 10 +++++++++ 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/PublicInbox/IMAP.pm b/lib/PublicInbox/IMAP.pm index 917833f7c47..3c32d846508 100644 --- a/lib/PublicInbox/IMAP.pm +++ b/lib/PublicInbox/IMAP.pm @@ -888,38 +888,35 @@ sub long_response ($$;@) { sub event_step { my ($self) = @_; - return unless $self->flush_write && $self->{sock}; + return unless $self->flush_write && $self->{sock} && !$self->{long_cb}; $self->update_idle_time; # only read more requests if we've drained the write buffer, # otherwise we can be buffering infinitely w/o backpressure - my $rbuf = $self->{rbuf} // (\(my $x = '')); - my $r = 1; - - if (index($$rbuf, "\n") < 0) { - my $off = length($$rbuf); - $r = $self->do_read($rbuf, LINE_MAX, $off) or return; - } - while ($r > 0 && $$rbuf =~ s/\A[ \t]*([^\n]*?)\r?\n//) { - my $line = $1; - return $self->close if $line =~ /[[:cntrl:]]/s; - my $t0 = now(); - my $fd = fileno($self->{sock}); - $r = eval { process_line($self, $line) }; - my $pending = $self->{wbuf} ? ' pending' : ''; - out($self, "[$fd] %s - %0.6f$pending", $line, now() - $t0); - } + my $rbuf = $self->{rbuf} // \(my $x = ''); + my $line = index($$rbuf, "\n"); + while ($line < 0) { + return $self->close if length($$rbuf) >= LINE_MAX; + $self->do_read($rbuf, LINE_MAX, length($$rbuf)) or return; + $line = index($$rbuf, "\n"); + } + $line = substr($$rbuf, 0, $line + 1, ''); + $line =~ s/\r?\n\z//s; + return $self->close if $line =~ /[[:cntrl:]]/s; + my $t0 = now(); + my $fd = fileno($self->{sock}); + my $r = eval { process_line($self, $line) }; + my $pending = $self->{wbuf} ? ' pending' : ''; + out($self, "[$fd] %s - %0.6f$pending - $r", $line, now() - $t0); return $self->close if $r < 0; - my $len = length($$rbuf); - return $self->close if ($len >= LINE_MAX); $self->rbuf_idle($rbuf); $self->update_idle_time; # maybe there's more pipelined data, or we'll have # to register it for socket-readiness notifications - $self->requeue unless $self->{wbuf}; + $self->requeue unless $pending; } sub compressed { undef } diff --git a/lib/PublicInbox/IMAPdeflate.pm b/lib/PublicInbox/IMAPdeflate.pm index 67c9a9738d5..42daa6cffaa 100644 --- a/lib/PublicInbox/IMAPdeflate.pm +++ b/lib/PublicInbox/IMAPdeflate.pm @@ -59,6 +59,16 @@ sub do_read ($$$$) { $doff = length($dbuf); my $r = PublicInbox::DS::do_read($self, \$dbuf, $len, $doff) or return; + # Workaround inflate bug appending to OOK scalars: + # + # We only have $off if the client is pipelining, and pipelining + # is where our substr() OOK optimization in event_step makes sense. + if ($off) { + my $copy = $$rbuf; + undef $$rbuf; + $$rbuf = $copy; + } + # assert(length($$rbuf) == $off) as far as NNTP.pm is concerned # -ConsumeInput is true, so $dbuf is automatically emptied my $err = $zin->inflate($dbuf, $rbuf);