From c9be556d34bb1696226ed4f1a5ffe945888d09d2 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 19 Aug 2025 00:33:34 +0000 Subject: [PATCH] ipc: get rid of -ipc_ppid field in favor of fork_gen We own all the `fork' calls in our codebase, so rely on the existing $PublicInbox::OnDestroy::fork_gen instead of using `$$' to call getpid(2) everywhere. getpid(2) is slower nowadays since it's always a syscall on modern glibc whereas it was cached (somewhat unsafely) in the past. --- lib/PublicInbox/IPC.pm | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/PublicInbox/IPC.pm b/lib/PublicInbox/IPC.pm index 9e4fa96b4..db313104d 100644 --- a/lib/PublicInbox/IPC.pm +++ b/lib/PublicInbox/IPC.pm @@ -100,8 +100,8 @@ sub exit_exception { exit(!!$@) } # starts a worker if Sereal or Storable is installed sub ipc_worker_spawn { my ($self, $ident, $oldset, $fields, @cb_args) = @_; - return if ($self->{-ipc_ppid} // -1) == $$; # idempotent - delete(@$self{qw(-ipc_req -ipc_res -ipc_ppid -ipc_pid)}); + return if $self->{-ipc_res} && $self->{-ipc_res}->can_reap; # idempotent + delete(@$self{qw(-ipc_req -ipc_res -ipc_pid)}); pipe(my $r_req, my $w_req); pipe(my $r_res, my $w_res); my $sigset = $oldset // PublicInbox::DS::block_signals(); @@ -132,7 +132,6 @@ sub ipc_worker_spawn { $self->{-ipc_req} = $w_req; $self->{-ipc_res} = PublicInbox::IO::attach_pid($r_res, $pid, \&ipc_worker_reap, $self, @cb_args); - $self->{-ipc_ppid} = $$; $self->{-ipc_pid} = $pid; } @@ -160,7 +159,7 @@ sub ipc_atfork_child { # idempotent, can be called regardless of whether worker is active or not sub ipc_worker_stop { my ($self) = @_; - my ($pid, $ppid) = delete(@$self{qw(-ipc_pid -ipc_ppid)}); + my $pid = delete $self->{-ipc_pid}; my ($w_req, $r_res) = delete(@$self{qw(-ipc_req -ipc_res)}); if (!$w_req && !$r_res) { die "unexpected PID:$pid without IPC pipes" if $pid; @@ -198,7 +197,7 @@ sub ipc_do { # causes newer siblings to inherit older siblings sockets sub ipc_sibling_atfork_child { my ($self) = @_; - my ($pid, undef) = delete(@$self{qw(-ipc_pid -ipc_ppid)}); + my $pid = delete $self->{-ipc_pid}; delete(@$self{qw(-ipc_req -ipc_res)}); $pid == $$ and die "BUG: $$ ipc_atfork_child called on itself"; } -- 2.47.3