From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id BAD7F1F4B4 for ; Sat, 7 Oct 2023 21:24:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1696713851; bh=TYnez4UqwPRzVIhLAFZKUyZ5lS+GaDxDor4G/eYQsI0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Y7+t/Hv0Su7ZrOEkibdSCIHvSsMeL10GLNvbgoX+jdITpZgjH6Jxjvl1cxKPNTgMp Eh1pNDXAEuusPbWyG+vV8M6n84xcYFuAv7MV5OXiVJD4xBk6pzZ5fVO7aIMNy15mjQ rfEbPGFJZztfbPFvq94iad0sDYC9s3cVdjdM6eIo= From: Eric Wong 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 Message-ID: <20231007212410.297785-9-e@80x24.org> In-Reply-To: <20231007212410.297785-1-e@80x24.org> References: <20231007212410.297785-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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) };