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 2/2] introduce ProcessIONBF for multiplexed non-blocking IO
Date: Sun,  8 Oct 2023 22:11:48 +0000	[thread overview]
Message-ID: <20231008221148.1792219-3-e@80x24.org> (raw)
In-Reply-To: <20231008221148.1792219-1-e@80x24.org>

This is required for reliable epoll/kevent/poll/select
wakeup notifications, since we have no visibility into
the buffer states used internally by Perl.

We can safely use sysread here since we never use the :utf8
nor any :encoding Perl IO layers for readable pipes.

I suspect this fixes occasional failures from t/solver_git.t
when retrieving the WwwCoderepo summary.
---
 MANIFEST                        |  1 +
 lib/PublicInbox/Git.pm          |  9 ++++-----
 lib/PublicInbox/HTTPD/Async.pm  |  4 ++--
 lib/PublicInbox/ProcessIONBF.pm | 25 +++++++++++++++++++++++++
 4 files changed, 32 insertions(+), 7 deletions(-)
 create mode 100644 lib/PublicInbox/ProcessIONBF.pm

diff --git a/MANIFEST b/MANIFEST
index c972818f..791d91a7 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -319,6 +319,7 @@ lib/PublicInbox/POP3.pm
 lib/PublicInbox/POP3D.pm
 lib/PublicInbox/PktOp.pm
 lib/PublicInbox/ProcessIO.pm
+lib/PublicInbox/ProcessIONBF.pm
 lib/PublicInbox/Qspawn.pm
 lib/PublicInbox/Reply.pm
 lib/PublicInbox/RepoAtom.pm
diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm
index 94d5dcee..448cfaf7 100644
--- a/lib/PublicInbox/Git.pm
+++ b/lib/PublicInbox/Git.pm
@@ -12,7 +12,6 @@ use v5.10.1;
 use parent qw(Exporter PublicInbox::DS);
 use autodie qw(socketpair);
 use POSIX ();
-use IO::Handle; # ->blocking
 use Socket qw(AF_UNIX SOCK_STREAM);
 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
 use Errno qw(EINTR EAGAIN);
@@ -20,6 +19,7 @@ use File::Glob qw(bsd_glob GLOB_NOSORT);
 use File::Spec ();
 use Time::HiRes qw(stat);
 use PublicInbox::Spawn qw(spawn popen_rd which);
+use PublicInbox::ProcessIONBF;
 use PublicInbox::Tmpfile;
 use IO::Poll qw(POLLIN);
 use Carp qw(croak carp);
@@ -146,7 +146,6 @@ sub _sock_cmd {
 	my ($self, $batch, $err_c) = @_;
 	$self->{sock} and Carp::confess('BUG: {sock} exists');
 	socketpair(my $s1, my $s2, AF_UNIX, SOCK_STREAM, 0);
-	$s1->blocking(0);
 	my $opt = { pgid => 0, 0 => $s2, 1 => $s2 };
 	my $gd = $self->{git_dir};
 	if ($gd =~ s!/([^/]+/[^/]+)\z!/!) {
@@ -165,7 +164,7 @@ sub _sock_cmd {
 						$self->fail("tmpfile($id): $!");
 	}
 	my $pid = spawn(\@cmd, undef, $opt);
-	$self->{sock} = PublicInbox::ProcessIO->maybe_new($pid, $s1);
+	$self->{sock} = PublicInbox::ProcessIONBF->new($pid, $s1);
 }
 
 sub poll_in ($) { IO::Poll::_poll($RDTIMEO, fileno($_[0]), my $ev = POLLIN) }
@@ -626,8 +625,8 @@ sub cleanup_if_unlinked {
 	my $ret = 0;
 	for my $obj ($self, ($self->{ck} // ())) {
 		my $sock = $obj->{sock} // next;
-		my PublicInbox::ProcessIO $pp = tied *$sock; # ProcessIO
-		my $pid = $pp->{pid} // next;
+		my PublicInbox::ProcessIONBF $p = tied *$sock; # ProcessIONBF
+		my $pid = $p->{pid} // next;
 		open my $fh, '<', "/proc/$pid/maps" or return cleanup($self, 1);
 		while (<$fh>) {
 			# n.b. we do not restart for unlinked multi-pack-index
diff --git a/lib/PublicInbox/HTTPD/Async.pm b/lib/PublicInbox/HTTPD/Async.pm
index b9d2159c..b73d0c4b 100644
--- a/lib/PublicInbox/HTTPD/Async.pm
+++ b/lib/PublicInbox/HTTPD/Async.pm
@@ -18,6 +18,7 @@ use v5.12;
 use parent qw(PublicInbox::DS);
 use Errno qw(EAGAIN);
 use PublicInbox::Syscall qw(EPOLLIN);
+use PublicInbox::ProcessIONBF;
 
 # This is called via: $env->{'pi-httpd.async'}->()
 # $io is a read-only pipe ($rpipe) for now, but may be a
@@ -37,8 +38,7 @@ sub new {
 		arg => $arg, # arg for $cb
 		end_obj => $end_obj, # like END{}, can ->event_step
 	}, $class;
-	my $pp = tied *$io; # ProcessIO
-	$pp->{fh}->blocking(0) // die "$io->blocking(0): $!";
+	PublicInbox::ProcessIONBF->replace($io);
 	$self->SUPER::new($io, EPOLLIN);
 }
 
diff --git a/lib/PublicInbox/ProcessIONBF.pm b/lib/PublicInbox/ProcessIONBF.pm
new file mode 100644
index 00000000..490e200a
--- /dev/null
+++ b/lib/PublicInbox/ProcessIONBF.pm
@@ -0,0 +1,25 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# used to support unbuffered partial reads
+package PublicInbox::ProcessIONBF;
+use v5.12;
+use parent qw(PublicInbox::ProcessIO);
+use IO::Handle; # ->blocking
+
+sub new {
+	my ($cls, $pid, $fh, @cb_arg) = @_;
+	$fh->blocking(0) // die "$fh->blocking(0): $!";
+	my $io = $cls->SUPER::maybe_new($pid, $fh, @cb_arg);
+}
+
+sub replace {
+	my ($cls, $orig) = @_;
+	my $pio = tied *$orig; # ProcessIO
+	$pio->{fh}->blocking(0) // die "$pio->{fh}->blocking(0): $!";
+	bless $pio, $cls;
+}
+
+sub READ { sysread($_[0]->{fh}, $_[1], $_[2], $_[3] // 0) }
+
+1;

      parent reply	other threads:[~2023-10-08 22:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-08 22:11 [PATCH 0/2] process IO fixes Eric Wong
2023-10-08 22:11 ` [PATCH 1/2] process_io: fix binmode and use it in lei_xsearch Eric Wong
2023-10-08 22:11 ` Eric Wong [this message]

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: https://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=20231008221148.1792219-3-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).