From a4cf3bd57d17577b6dd791d4f0f8e76758d0aafe Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 17 Oct 2023 23:37:59 +0000 Subject: ds: introduce and use do_fork helper This ensures we handle RNG reseeding and resetting the event loop properly in child processes after forking. --- lib/PublicInbox/Daemon.pm | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'lib/PublicInbox/Daemon.pm') diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm index 520cef72..f33f6f17 100644 --- a/lib/PublicInbox/Daemon.pm +++ b/lib/PublicInbox/Daemon.pm @@ -541,17 +541,11 @@ sub reap_worker { # awaitpid CB sub start_worker ($) { my ($nr) = @_; - my $seed = rand(0xffffffff); return unless @listeners; - my $pid = fork; - if (!defined($pid)) { - warn "fork: $!"; - } elsif ($pid == 0) { + my $pid = PublicInbox::DS::do_fork; + if ($pid == 0) { undef %WORKERS; - PublicInbox::DS::Reset(); local $PublicInbox::DS::Poller; # allow epoll/kqueue - srand($seed); - eval { Net::SSLeay::randomize() }; $set_user->() if $set_user; PublicInbox::EOFpipe->new($parent_pipe, \&worker_quit); worker_loop(); @@ -563,9 +557,9 @@ sub start_worker ($) { } sub start_workers { - for my $nr (grep { !defined($WORKERS{$_}) } (0..($nworker - 1))) { - start_worker($nr); - } + my @idx = grep { !defined($WORKERS{$_}) } (0..($nworker - 1)) or return; + eval { start_worker($_) for @idx }; + warn "E: $@\n" if $@; } sub trim_workers { -- cgit v1.2.3-24-ge0c7