user/dev discussion of public-inbox itself
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] gcf2: improve error handling and do not ->fail on wbuf
Date: Sun, 27 Sep 2020 22:12:48 +0000
Message-ID: <20200927221248.28815-1-e@80x24.org> (raw)

For historical reasons, both Danga::Socket::write and
PublicInbox::DS::write will return 0 when data is buffered;
so we Gcf2Client must not call ->fail when DS::write returns 0.

We'll also improve robustness by recreating the entire
Gcf2Client object if it does die for other reasons, instead of
risking mismatched fields.

We also need to ensure we only get one EPOLLERR wakeup and
issue EPOLL_CTL_DEL if ->event_step is triggered by a dying
Gcf2 process.
---
 lib/PublicInbox/Daemon.pm      | 10 ++--------
 lib/PublicInbox/Gcf2Client.pm  | 31 +++++++++++++++++++------------
 lib/PublicInbox/GitAsyncCat.pm | 11 +++++++++--
 3 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index 1520f8f2..155707e1 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -654,14 +654,8 @@ sub run ($$$;$) {
 	my $af_default = $default =~ /:8080\z/ ? 'httpready' : undef;
 	my $for_destroy = daemonize();
 
-	# this wastes a bit of memory for non-PublicInbox::WWW -httpd users
-	# oh well...
-	eval {
-		require PublicInbox::Gcf2;
-		require PublicInbox::Gcf2Client;
-	};
-	local $PublicInbox::GitAsyncCat::GCF2C =
-				PublicInbox::Gcf2Client::new() if !$@;
+	# localize GCF2C for tests:
+	local $PublicInbox::GitAsyncCat::GCF2C;
 
 	daemon_loop($refresh, $post_accept, $tlsd, $af_default);
 	PublicInbox::DS->Reset;
diff --git a/lib/PublicInbox/Gcf2Client.pm b/lib/PublicInbox/Gcf2Client.pm
index 42ff1bf3..ab486de5 100644
--- a/lib/PublicInbox/Gcf2Client.pm
+++ b/lib/PublicInbox/Gcf2Client.pm
@@ -8,27 +8,28 @@ use parent qw(PublicInbox::DS);
 use PublicInbox::Git;
 use PublicInbox::Spawn qw(popen_rd);
 use IO::Handle ();
-use PublicInbox::Syscall qw(EPOLLONESHOT EPOLLOUT);
+use PublicInbox::Syscall qw(EPOLLONESHOT);
 # fields:
 #	async_cat => GitAsyncCat ref (read-only pipe)
 #	sock => writable pipe to Gcf2::loop
 
-sub new { bless($_[0] // {}, __PACKAGE__) }
 
-sub gcf2c_begin ($) {
-	my ($self) = @_;
+sub new  {
+	my ($rdr) = @_;
+	my $self = bless {}, __PACKAGE__;
 	# ensure the child process has the same @INC we do:
 	my $env = { PERL5LIB => join(':', @INC) };
 	my ($out_r, $out_w);
 	pipe($out_r, $out_w) or die "pipe failed: $!";
-	my $rdr = { 0 => $out_r, 2 => $self->{2} };
+	$rdr //= {};
+	$rdr->{0} = $out_r;
 	my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop()]];
 	@$self{qw(in pid)} = popen_rd($cmd, $env, $rdr);
 	fcntl($out_w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
 	$out_w->autoflush(1);
 	$out_w->blocking(0);
-	$self->SUPER::new($out_w, 0); # EPOLL_CTL_ADD (a bit wasteful :x)
 	$self->{inflight} = [];
+	$self->SUPER::new($out_w, EPOLLONESHOT); # detect errors once
 }
 
 sub fail {
@@ -39,22 +40,28 @@ sub fail {
 
 sub cat_async ($$$;$) {
 	my ($self, $req, $cb, $arg) = @_;
-	my $inflight = $self->{inflight} // gcf2c_begin($self);
+	my $inflight = $self->{inflight};
 
-	# rare, I hope:
+	# {wbuf} is rare, I hope:
 	cat_async_step($self, $inflight) if $self->{wbuf};
 
-	$self->write(\"$req\n") or $self->fail("gcf2c write: $!");
+	if (!$self->write(\"$req\n")) {
+		$self->fail("gcf2c write: $!") if !$self->{sock};
+	}
 	push @$inflight, $req, $cb, $arg;
 }
 
 # ensure PublicInbox::Git::cat_async_step never calls cat_async_retry
 sub alternates_changed {}
 
-no warnings 'once';
-
 # this is the write-only end of a pipe, DS->EventLoop will call this
-*event_step = \&PublicInbox::DS::flush_write;
+sub event_step {
+	my ($self) = @_;
+	$self->flush_write;
+	$self->close if !$self->{in}; # process died
+}
+
+no warnings 'once';
 
 # used by GitAsyncCat
 *cat_async_step = \&PublicInbox::Git::cat_async_step;
diff --git a/lib/PublicInbox/GitAsyncCat.pm b/lib/PublicInbox/GitAsyncCat.pm
index b9dbe0cc..be51f673 100644
--- a/lib/PublicInbox/GitAsyncCat.pm
+++ b/lib/PublicInbox/GitAsyncCat.pm
@@ -53,14 +53,21 @@ sub event_step {
 
 sub git_async_cat ($$$$) {
 	my ($git, $oid, $cb, $arg) = @_;
-	my $gitish = $GCF2C;
-	if ($gitish) {
+	my $gitish = $GCF2C //= eval {
+		require PublicInbox::Gcf2;
+		require PublicInbox::Gcf2Client;
+		PublicInbox::Gcf2Client::new();
+	} // 0; # 0: do not retry if libgit2 or Inline::C are missing
+	if ($gitish) { # Gcf2 active, {inflight} may be unset due to errors
+		$GCF2C->{inflight} or
+			$gitish = $GCF2C = PublicInbox::Gcf2Client::new();
 		$oid .= " $git->{git_dir}";
 	} else {
 		$gitish = $git;
 	}
 	$gitish->cat_async($oid, $cb, $arg);
 	$gitish->{async_cat} //= do {
+		# read-only end of pipe (Gcf2Client is write-only end)
 		my $self = bless { gitish => $gitish }, __PACKAGE__;
 		$self->SUPER::new($gitish->{in}, EPOLLIN|EPOLLET);
 		\undef; # this is a true ref()

             reply	other threads:[~2020-09-27 22:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-27 22:12 Eric Wong [this message]
2020-09-28  5:52 ` Eric Wong

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=20200927221248.28815-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

user/dev discussion of public-inbox itself

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 meta meta/ http://public-inbox.org/meta \
		meta@public-inbox.org
	public-inbox-index meta

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git