about summary refs log tree commit homepage
path: root/lib/PublicInbox/Gcf2Client.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2020-09-27 22:12:48 +0000
committerEric Wong <e@80x24.org>2020-09-28 05:48:14 +0000
commit8ba04f214bbadcbe106c94281a0c4c21dd50adb8 (patch)
treea225cd4a836ead39d6b1f2dc4a72f81409684db6 /lib/PublicInbox/Gcf2Client.pm
parent28dec3a76687617a3a697058964dc0bd1351eb6b (diff)
downloadpublic-inbox-8ba04f214bbadcbe106c94281a0c4c21dd50adb8.tar.gz
gcf2: improve error handling and do not ->fail on wbuf
For historical reasons, both Danga::Socket::write and
PublicInbox::DS::write will return 0 when data is buffered;
so 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 due to deferred close.

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, so always register the FD with EPOLLONESHOT.
Diffstat (limited to 'lib/PublicInbox/Gcf2Client.pm')
-rw-r--r--lib/PublicInbox/Gcf2Client.pm31
1 files changed, 19 insertions, 12 deletions
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;