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 3/9] ipc: get rid of wq_set_recv_modes
Date: Sun, 24 Jan 2021 04:46:49 -0700	[thread overview]
Message-ID: <20210124114655.12815-4-e@80x24.org> (raw)
In-Reply-To: <20210124114655.12815-1-e@80x24.org>

Just open every FD as read/write.  Perl (or any non-broken
runtime) won't care and won't attempt to use F_SETFL to alter
file description flags; as attempting to change those would
lead to unpleasant side effects if the file description is
shared with another process.
---
 lib/PublicInbox/IPC.pm        | 11 ++---------
 lib/PublicInbox/LEI.pm        |  7 +++----
 lib/PublicInbox/LeiToMail.pm  |  7 ++-----
 lib/PublicInbox/LeiXSearch.pm |  5 ++---
 script/lei                    | 11 +++++------
 t/ipc.t                       |  1 -
 6 files changed, 14 insertions(+), 28 deletions(-)

diff --git a/lib/PublicInbox/IPC.pm b/lib/PublicInbox/IPC.pm
index 592efd21..c52441f7 100644
--- a/lib/PublicInbox/IPC.pm
+++ b/lib/PublicInbox/IPC.pm
@@ -46,11 +46,6 @@ my $send_cmd = PublicInbox::Spawn->can('send_cmd4') // do {
 	PublicInbox::CmdIPC4->can('send_cmd4');
 };
 
-sub wq_set_recv_modes {
-	my ($self, @modes) = @_;
-	$self->{-wq_recv_modes} = \@modes;
-}
-
 sub _get_rec ($) {
 	my ($r) = @_;
 	defined(my $len = <$r>) or return;
@@ -251,15 +246,13 @@ sub _recv_and_run {
 	my ($self, $s2, $len, $full_stream) = @_;
 	my @fds = $recv_cmd->($s2, my $buf, $len);
 	my $n = length($buf // '') or return;
-	my @m = @{$self->{-wq_recv_modes} // [qw( +<&= >&= >&= )]};
 	my $nfd = 0;
 	for my $fd (@fds) {
-		my $mode = shift(@m);
-		if (open(my $cmdfh, $mode, $fd)) {
+		if (open(my $cmdfh, '+<&=', $fd)) {
 			$self->{$nfd++} = $cmdfh;
 			$cmdfh->autoflush(1);
 		} else {
-			die "$$ open($mode$fd) (FD:$nfd): $!";
+			die "$$ open(+<&=$fd) (FD:$nfd): $!";
 		}
 	}
 	while ($full_stream && $n < $len) {
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index a9123c6e..473a28a9 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -765,11 +765,10 @@ sub accept_dispatch { # Listener {post_accept} callback
 		return send($sock, 'timed out waiting to recv FDs', MSG_EOR);
 	my @fds = $recv_cmd->($sock, my $buf, 4096 * 33); # >MAX_ARG_STRLEN
 	if (scalar(@fds) == 4) {
-		my $i = 0;
-		for my $rdr (qw(<&= >&= >&= <&=)) {
+		for my $i (0..3) {
 			my $fd = shift(@fds);
-			open($self->{$i++}, $rdr, $fd) and next;
-			send($sock, "open($rdr$fd) (FD=$i): $!", MSG_EOR);
+			open($self->{$i}, '+<&=', $fd) and next;
+			send($sock, "open(+<&=$fd) (FD=$i): $!", MSG_EOR);
 		}
 	} else {
 		return send($sock, "recv_cmd failed: $!", MSG_EOR);
diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm
index 5f38add1..08a1570d 100644
--- a/lib/PublicInbox/LeiToMail.pm
+++ b/lib/PublicInbox/LeiToMail.pm
@@ -227,9 +227,7 @@ sub decompress_src ($$$) {
 
 sub dup_src ($) {
 	my ($in) = @_;
-	# fileno needed because wq_set_recv_modes only used ">&=" for {1}
-	# and Perl blindly trusts that to reject the '+' (readability flag)
-	open my $dup, '+>>&=', fileno($in) or die "dup: $!";
+	open my $dup, '+>>&', $in or die "dup: $!";
 	$dup;
 }
 
@@ -475,8 +473,7 @@ sub write_mail { # via ->wq_do
 
 sub ipc_atfork_prepare {
 	my ($self) = @_;
-	# (done_wr, stdout|mbox, stderr, 3: sock, 4: each_smsg_done_wr)
-	$self->wq_set_recv_modes(qw[+<&= >&= >&= +<&= >&=]);
+	# FDs: (done_wr, stdout|mbox, stderr, 3: sock, 4: each_smsg_done_wr)
 	$self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC
 }
 
diff --git a/lib/PublicInbox/LeiXSearch.pm b/lib/PublicInbox/LeiXSearch.pm
index 1c093a94..c396c597 100644
--- a/lib/PublicInbox/LeiXSearch.pm
+++ b/lib/PublicInbox/LeiXSearch.pm
@@ -389,9 +389,8 @@ sub ipc_atfork_prepare {
 		require PublicInbox::MboxReader;
 		require IO::Uncompress::Gunzip;
 	}
-	# (0: done_wr, 1: stdout|mbox, 2: stderr,
-	#  3: sock, 4: $l2m->{-wq_s1}, 5: $startq)
-	$self->wq_set_recv_modes(qw[+<&= >&= >&= +<&= +<&= <&=]);
+	# FDS: (0: done_wr, 1: stdout|mbox, 2: stderr,
+	#       3: sock, 4: $l2m->{-wq_s1}, 5: $startq)
 	$self->SUPER::ipc_atfork_prepare; # PublicInbox::IPC
 }
 
diff --git a/script/lei b/script/lei
index 8c40bf12..006c1180 100755
--- a/script/lei
+++ b/script/lei
@@ -23,20 +23,19 @@ sub sigchld {
 
 sub exec_cmd {
 	my ($fds, $argc, @argv) = @_;
-	my @m = (*STDIN{IO}, '<&=',  *STDOUT{IO}, '>&=', *STDERR{IO}, '>&=');
+	my @old = (*STDIN{IO}, *STDOUT{IO}, *STDERR{IO});
 	my @rdr;
 	for my $fd (@$fds) {
-		my ($old_io, $mode) = splice(@m, 0, 2);
-		open(my $tmpfh, $mode, $fd) or die "open $mode$fd: $!";
-		push @rdr, $old_io, $mode, $tmpfh;
+		open(my $tmpfh, '+<&=', $fd) or die "open +<&=$fd: $!";
+		push @rdr, shift(@old), $tmpfh;
 	}
 	require POSIX; # WNOHANG
 	$SIG{CHLD} = \&sigchld;
 	my $pid = fork // die "fork: $!";
 	if ($pid == 0) {
 		my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
-		while (my ($old_io, $mode, $tmpfh) = splice(@rdr, 0, 3)) {
-			open $old_io, $mode, $tmpfh or die "open $mode: $!";
+		while (my ($old_io, $tmpfh) = splice(@rdr, 0, 2)) {
+			open $old_io, '+<&', $tmpfh or die "open +<&=: $!";
 		}
 		%ENV = (%ENV, %env);
 		exec(@argv);
diff --git a/t/ipc.t b/t/ipc.t
index f25f2491..5801c760 100644
--- a/t/ipc.t
+++ b/t/ipc.t
@@ -119,7 +119,6 @@ $test->('local');
 $ipc->ipc_worker_stop; # idempotent
 
 # work queues
-$ipc->wq_set_recv_modes(qw( +>&= >&= >&= ));
 pipe(my ($ra, $wa)) or BAIL_OUT $!;
 pipe(my ($rb, $wb)) or BAIL_OUT $!;
 pipe(my ($rc, $wc)) or BAIL_OUT $!;

  parent reply	other threads:[~2021-01-24 11:46 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-24 11:46 [PATCH 0/9] lei remotes fixes and updates Eric Wong
2021-01-24 11:46 ` [PATCH 1/9] lei q: limit concurrency to 4 remote connections Eric Wong
2021-01-24 11:46 ` [PATCH 2/9] ipc: wq supports arbitrarily large payloads Eric Wong
2021-01-24 11:46 ` Eric Wong [this message]
2021-01-24 11:46 ` [PATCH 4/9] lei q: disable remote externals if locals exist Eric Wong
2021-01-24 11:46 ` [PATCH 5/9] lei q: honor --no-local to force remote searches Eric Wong
2021-01-24 12:31   ` exit codes [was: [PATCH 5/9] lei q: honor --no-local to force remote searches] Eric Wong
2021-01-24 11:46 ` [PATCH 6/9] lei_xsearch: use curl -d '' for nginx compatibility Eric Wong
2021-01-24 11:46 ` [PATCH 7/9] lei q: fix JSON overview with remote externals Eric Wong
2021-01-24 12:37   ` Eric Wong
2021-01-24 11:46 ` [PATCH 8/9] smsg: make parse_references an object method Eric Wong
2021-01-24 11:46 ` [PATCH 9/9] smsg: parse_references: micro-optimization 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: 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=20210124114655.12815-4-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).