From 4a2a95bbc78f99c8c5278cfe29de74bd1483903c Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 17 Jan 2023 07:19:10 +0000 Subject: ipc+lei: switch to awaitpid This avoids awkwardly stuffing an arrayref into callbacks which expect multiple arguments. IPC->awaitpid_init now allows pre-registering callbacks before spawning workers. --- lib/PublicInbox/IPC.pm | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'lib/PublicInbox/IPC.pm') diff --git a/lib/PublicInbox/IPC.pm b/lib/PublicInbox/IPC.pm index 34e40118..edc5ba64 100644 --- a/lib/PublicInbox/IPC.pm +++ b/lib/PublicInbox/IPC.pm @@ -12,7 +12,7 @@ use strict; use v5.10.1; use parent qw(Exporter); use Carp qw(croak); -use PublicInbox::DS qw(dwaitpid); +use PublicInbox::DS qw(awaitpid); use PublicInbox::Spawn; use PublicInbox::OnDestroy; use PublicInbox::WQWorker; @@ -133,26 +133,26 @@ sub ipc_worker_spawn { $self->{-ipc_req} = $w_req; $self->{-ipc_res} = $r_res; $self->{-ipc_ppid} = $$; + awaitpid($pid, \&ipc_worker_reap, $self); $self->{-ipc_pid} = $pid; } -sub ipc_worker_reap { # dwaitpid callback - my ($args, $pid) = @_; - my ($self, @uargs) = @$args; +sub ipc_worker_reap { # awaitpid callback + my ($pid, $self) = @_; delete $self->{-wq_workers}->{$pid}; - return $self->{-reap_do}->($args, $pid) if $self->{-reap_do}; + if (my $cb_args = $self->{-reap_do}) { + return $cb_args->[0]->($pid, $self, @$cb_args[1..$#$cb_args]); + } return if !$?; my $s = $? & 127; # TERM(15) is our default exit signal, PIPE(13) is likely w/ pager warn "$self->{-wq_ident} PID:$pid died \$?=$?\n" if $s != 15 && $s != 13 } -sub wq_wait_async { - my ($self, $cb, @uargs) = @_; - local $PublicInbox::DS::in_loop = 1; - $self->{-reap_do} = $cb; - my @pids = keys %{$self->{-wq_workers}}; - dwaitpid($_, \&ipc_worker_reap, [ $self, @uargs ]) for @pids; +# register wait workers +sub awaitpid_init { + my ($self, @cb_args) = @_; + $self->{-reap_do} = \@cb_args; } # for base class, override in sub classes @@ -178,9 +178,7 @@ sub ipc_worker_stop { } die 'no PID with IPC pipes' unless $pid; $w_req = $r_res = undef; - - return if $$ != $ppid; - dwaitpid($pid, \&ipc_worker_reap, [$self]); + awaitpid($pid) if $$ == $ppid; # for non-event loop } # use this if we have multiple readers reading curl or "pigz -dc" @@ -397,6 +395,7 @@ sub _wq_worker_start ($$$$) { undef $end; # trigger exit } else { $self->{-wq_workers}->{$pid} = $bcast1; + awaitpid($pid, \&ipc_worker_reap, $self); } } @@ -428,8 +427,7 @@ sub wq_close { } delete @$self{qw(-wq_s1 -wq_s2)} or return; return if $self->{-reap_do}; - my @pids = keys %{$self->{-wq_workers}}; - dwaitpid($_, \&ipc_worker_reap, [ $self ]) for @pids; + awaitpid($_) for keys %{$self->{-wq_workers}}; } sub wq_kill { -- cgit v1.2.3-24-ge0c7