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 8/9] process_io: pass args to awaitpid as list
Date: Sat,  7 Oct 2023 21:24:09 +0000	[thread overview]
Message-ID: <20231007212410.297785-9-e@80x24.org> (raw)
In-Reply-To: <20231007212410.297785-1-e@80x24.org>

Specifying {cb_args} in the options hash felt awkward to me.
Instead, just use the Perl stack like we do with awaitpid()
and pass the list down directly.
---
 lib/PublicInbox/LeiToMail.pm |  4 ++--
 lib/PublicInbox/ProcessIO.pm |  4 ++--
 lib/PublicInbox/Qspawn.pm    |  4 ++--
 lib/PublicInbox/Spawn.pm     | 10 ++++++----
 t/spawn.t                    |  8 ++++----
 5 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/lib/PublicInbox/LeiToMail.pm b/lib/PublicInbox/LeiToMail.pm
index f56ad330..8771592d 100644
--- a/lib/PublicInbox/LeiToMail.pm
+++ b/lib/PublicInbox/LeiToMail.pm
@@ -162,8 +162,8 @@ sub _post_augment_mbox { # open a compressor process from top-level lei-daemon
 	my ($r, $w) = @{delete $lei->{zpipe}};
 	my $rdr = { 0 => $r, 1 => $lei->{1}, 2 => $lei->{2}, pgid => 0 };
 	my $pid = spawn($cmd, undef, $rdr);
-	$lei->{1} = PublicInbox::ProcessIO->maybe_new($pid, $w, {
-			cb_arg => [\&reap_compress, $lei, $cmd, $lei->{1} ] });
+	$lei->{1} = PublicInbox::ProcessIO->maybe_new($pid, $w,
+				\&reap_compress, $lei, $cmd, $lei->{1});
 }
 
 # --augment existing output destination, with deduplication
diff --git a/lib/PublicInbox/ProcessIO.pm b/lib/PublicInbox/ProcessIO.pm
index eeb66139..5a81e3a6 100644
--- a/lib/PublicInbox/ProcessIO.pm
+++ b/lib/PublicInbox/ProcessIO.pm
@@ -9,10 +9,10 @@ use PublicInbox::DS qw(awaitpid);
 use Symbol qw(gensym);
 
 sub maybe_new {
-	my ($cls, $pid, $fh, $opt) = @_;
+	my ($cls, $pid, $fh, @cb_arg) = @_;
 	return ($fh, $pid) if wantarray;
 	my $s = gensym;
-	tie *$s, $cls, $pid, $fh, @{$opt->{cb_arg} // []};
+	tie *$s, $cls, $pid, $fh, @cb_arg;
 	$s;
 }
 
diff --git a/lib/PublicInbox/Qspawn.pm b/lib/PublicInbox/Qspawn.pm
index ea7ae647..0e52617c 100644
--- a/lib/PublicInbox/Qspawn.pm
+++ b/lib/PublicInbox/Qspawn.pm
@@ -58,10 +58,10 @@ sub _do_spawn {
 	}
 	$self->{cmd} = $cmd;
 	$self->{-quiet} = 1 if $o{quiet};
-	$o{cb_arg} = [ \&waitpid_err, $self ];
 	eval {
 		# popen_rd may die on EMFILE, ENFILE
-		$self->{rpipe} = popen_rd($cmd, $cmd_env, \%o) // die "E: $!";
+		$self->{rpipe} = popen_rd($cmd, $cmd_env, \%o,
+					\&waitpid_err, $self);
 		$limiter->{running}++;
 		$start_cb->($self); # EPOLL_CTL_ADD may ENOSPC/ENOMEM
 	};
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index cb8b21c6..ec256698 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -366,15 +366,17 @@ sub spawn ($;$$) {
 }
 
 sub popen_rd {
-	my ($cmd, $env, $opt) = @_;
+	my ($cmd, $env, $opt, @cb_arg) = @_;
 	pipe(my $r, local $opt->{1}) or die "pipe: $!\n";
-	PublicInbox::ProcessIO->maybe_new(spawn($cmd, $env, $opt), $r, $opt)
+	my $pid = spawn($cmd, $env, $opt);
+	PublicInbox::ProcessIO->maybe_new($pid, $r, @cb_arg);
 }
 
 sub popen_wr {
-	my ($cmd, $env, $opt) = @_;
+	my ($cmd, $env, $opt, @cb_arg) = @_;
 	pipe(local $opt->{0}, my $w) or die "pipe: $!\n";
-	PublicInbox::ProcessIO->maybe_new(spawn($cmd, $env, $opt), $w, $opt)
+	my $pid = spawn($cmd, $env, $opt);
+	PublicInbox::ProcessIO->maybe_new($pid, $w, @cb_arg)
 }
 
 sub run_wait ($;$$) {
diff --git a/t/spawn.t b/t/spawn.t
index be5aaf9f..1af66bda 100644
--- a/t/spawn.t
+++ b/t/spawn.t
@@ -140,13 +140,13 @@ EOF
 
 { # ->CLOSE vs ->DESTROY waitpid caller distinction
 	my @c;
-	my $fh = popen_rd(['true'], undef, { cb_arg => [sub { @c = caller }] });
+	my $fh = popen_rd(['true'], undef, undef, sub { @c = caller });
 	ok(close($fh), '->CLOSE fired and successful');
 	ok(scalar(@c), 'callback fired by ->CLOSE');
 	ok(grep(!m[/PublicInbox/DS\.pm\z], @c), 'callback not invoked by DS');
 
 	@c = ();
-	$fh = popen_rd(['true'], undef, { cb_arg => [sub { @c = caller }] });
+	$fh = popen_rd(['true'], undef, undef, sub { @c = caller });
 	undef $fh; # ->DESTROY
 	ok(scalar(@c), 'callback fired by ->DESTROY');
 	ok(grep(!m[/PublicInbox/ProcessIO\.pm\z], @c),
@@ -157,8 +157,8 @@ EOF
 	use POSIX qw(_exit);
 	pipe(my ($r, $w)) or BAIL_OUT $!;
 	my @arg;
-	my $cb = [ sub { @arg = @_; warn "x=$$\n" }, 'hi' ];
-	my $fh = popen_rd(['cat'], undef, { 0 => $r, cb_arg => $cb });
+	my $fh = popen_rd(['cat'], undef, { 0 => $r },
+			sub { @arg = @_; warn "x=$$\n" }, 'hi');
 	my $pp = tied *$fh;
 	my $pid = fork // BAIL_OUT $!;
 	local $SIG{__WARN__} = sub { _exit(1) };

  parent reply	other threads:[~2023-10-07 21:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-07 21:24 [PATCH 0/9] more process-related cleanups Eric Wong
2023-10-07 21:24 ` [PATCH 1/9] xt/httpd-async-stream: avoid waitpid call Eric Wong
2023-10-07 21:24 ` [PATCH 2/9] lei: do not issue sto->done if socket is inactive Eric Wong
2023-10-07 21:24 ` [PATCH 3/9] lei: always use async `done' requests to store Eric Wong
2023-10-08  1:58   ` Eric Wong
2023-10-08  5:49   ` [PATCH 2.5/9] lei: fix implicit stdin support for pipes Eric Wong
2023-10-08 18:54   ` [PATCHv2 3/9] lei: always use async `done' requests to store Eric Wong
2023-10-07 21:24 ` [PATCH 4/9] ipc: require fork+SOCK_SEQPACKET for wq_* functions Eric Wong
2023-10-07 21:24 ` [PATCH 5/9] ipc: use autodie for most syscalls Eric Wong
2023-10-07 21:24 ` [PATCH 6/9] import: use autodie, rely on PerlIO for retries Eric Wong
2023-10-07 21:24 ` [PATCH 7/9] rename ProcessPipe to ProcessIO Eric Wong
2023-10-07 21:24 ` Eric Wong [this message]
2023-10-07 21:24 ` [PATCH 9/9] cindex: start using autodie 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=20231007212410.297785-9-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).