]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
process_io: pass args to awaitpid as list
authorEric Wong <e@80x24.org>
Sat, 7 Oct 2023 21:24:09 +0000 (21:24 +0000)
committerEric Wong <e@80x24.org>
Sun, 8 Oct 2023 18:54:48 +0000 (18:54 +0000)
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
lib/PublicInbox/ProcessIO.pm
lib/PublicInbox/Qspawn.pm
lib/PublicInbox/Spawn.pm
t/spawn.t

index f56ad330d87f305d2a267c55bd6a5e4ad798794c..8771592d36a106b1a7fbce35b4be27a7fa9841bc 100644 (file)
@@ -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
index eeb66139be6621fa1e45879f6403db7245fd84a9..5a81e3a65aae5e45ee0399458a766b97ed2a8b89 100644 (file)
@@ -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;
 }
 
index ea7ae647014d0dbcf5fcd36c48d452ff857233cf..0e52617c317881787a5973762e101dfdfb4f8a08 100644 (file)
@@ -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
        };
index cb8b21c6ac57df39099b67fb9e75ccd93894589d..ec256698de589082c37d9f66a6a24d498f3c975a 100644 (file)
@@ -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 ($;$$) {
index be5aaf9fb90af8618b432b1eb89b5280084239c0..1af66bda6110b14bab763eea50b09dbcbabc3a72 100644 (file)
--- 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) };