user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH 14/30] ds: introduce and use do_fork helper
  2023-10-17 23:37  7% [PATCH 00/30] autodie-ification and code simplifications Eric Wong
@ 2023-10-17 23:37  6% ` Eric Wong
  0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2023-10-17 23:37 UTC (permalink / raw)
  To: meta

This ensures we handle RNG reseeding and resetting the event
loop properly in child processes after forking.
---
 lib/PublicInbox/DS.pm         | 12 ++++++++++++
 lib/PublicInbox/Daemon.pm     | 16 +++++-----------
 lib/PublicInbox/IPC.pm        | 12 ++----------
 lib/PublicInbox/TestCommon.pm |  3 +--
 lib/PublicInbox/Watch.pm      | 11 ++---------
 lib/PublicInbox/Xapcmd.pm     |  5 ++---
 6 files changed, 24 insertions(+), 35 deletions(-)

diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index eefbdcc3..9960937d 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -34,6 +34,7 @@ use PublicInbox::Tmpfile;
 use PublicInbox::Select;
 use Errno qw(EAGAIN EINVAL ECHILD);
 use Carp qw(carp croak);
+use autodie qw(fork);
 our @EXPORT_OK = qw(now msg_more awaitpid add_timer add_uniq_timer);
 
 my $nextq; # queue for next_tick
@@ -737,6 +738,17 @@ sub awaitpid {
 	}
 }
 
+sub do_fork () {
+	my $seed = rand(0xffffffff);
+	my $pid = fork;
+	if ($pid == 0) {
+		srand($seed);
+		eval { Net::SSLeay::randomize() };
+		Reset();
+	}
+	$pid;
+}
+
 package PublicInbox::DummyPoller; # only used during Reset
 use v5.12;
 
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 {
diff --git a/lib/PublicInbox/IPC.pm b/lib/PublicInbox/IPC.pm
index 5964645e..3292d960 100644
--- a/lib/PublicInbox/IPC.pm
+++ b/lib/PublicInbox/IPC.pm
@@ -102,12 +102,8 @@ sub ipc_worker_spawn {
 	pipe(my $r_res, my $w_res);
 	my $sigset = $oldset // PublicInbox::DS::block_signals();
 	$self->ipc_atfork_prepare;
-	my $seed = rand(0xffffffff);
-	my $pid = fork;
+	my $pid = PublicInbox::DS::do_fork;
 	if ($pid == 0) {
-		srand($seed);
-		eval { Net::SSLeay::randomize() };
-		eval { PublicInbox::DS->Reset };
 		delete @$self{qw(-wq_s1 -wq_s2 -wq_workers -wq_ppid)};
 		$w_req = $r_res = undef;
 		$w_res->autoflush(1);
@@ -341,13 +337,9 @@ sub _wq_worker_start {
 	my ($self, $oldset, $fields, $one, @cb_args) = @_;
 	my ($bcast1, $bcast2);
 	$one or socketpair($bcast1, $bcast2, AF_UNIX, SOCK_SEQPACKET, 0);
-	my $seed = rand(0xffffffff);
-	my $pid = fork;
+	my $pid = PublicInbox::DS::do_fork;
 	if ($pid == 0) {
-		srand($seed);
-		eval { Net::SSLeay::randomize() };
 		undef $bcast1;
-		eval { PublicInbox::DS->Reset };
 		delete @$self{qw(-wq_s1 -wq_ppid)};
 		$self->{-wq_worker_nr} =
 				keys %{delete($self->{-wq_workers}) // {}};
diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index 323152b4..77da822b 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -549,9 +549,8 @@ sub start_script {
 	require PublicInbox::OnDestroy;
 	my $tmp_mask = PublicInbox::OnDestroy->new(
 					\&PublicInbox::DS::sig_setmask, $oset);
-	my $pid = fork // die "fork: $!";
+	my $pid = PublicInbox::DS::do_fork();
 	if ($pid == 0) {
-		eval { PublicInbox::DS->Reset };
 		for (@{delete($opt->{-CLOFORK}) // []}) {
 			close($_) or die "close $!";
 		}
diff --git a/lib/PublicInbox/Watch.pm b/lib/PublicInbox/Watch.pm
index 3426d4a7..41b77dc1 100644
--- a/lib/PublicInbox/Watch.pm
+++ b/lib/PublicInbox/Watch.pm
@@ -385,7 +385,6 @@ sub watch_atfork_child ($) {
 	my ($self) = @_;
 	delete $self->{pids};
 	delete $self->{opendirs};
-	PublicInbox::DS->Reset;
 	my $sig = delete $self->{sig};
 	$sig->{CHLD} = $sig->{HUP} = $sig->{USR1} = 'DEFAULT';
 	# TERM/QUIT/INT call ->quit, which works in both parent+child
@@ -413,11 +412,8 @@ sub imap_idle_reap { # awaitpid callback
 sub imap_idle_fork {
 	my ($self, $uri, $intvl) = @_;
 	return if $self->{quit};
-	my $seed = rand(0xffffffff);
-	my $pid = fork // die "fork: $!";
+	my $pid = PublicInbox::DS::do_fork;
 	if ($pid == 0) {
-		srand($seed);
-		eval { Net::SSLeay::randomize() };
 		watch_atfork_child($self);
 		watch_imap_idle_1($self, $uri, $intvl);
 		_exit(0);
@@ -477,11 +473,8 @@ sub poll_fetch_fork { # DS::add_timer callback
 	my @imap = grep { # push() always returns > 0
 		$_->scheme =~ m!\Aimaps?!i ? 1 : (push(@nntp, $_) < 0)
 	} @$uris;
-	my $seed = rand(0xffffffff);
-	my $pid = fork // die "fork: $!";
+	my $pid = PublicInbox::DS::do_fork;
 	if ($pid == 0) {
-		srand($seed);
-		eval { Net::SSLeay::randomize() };
 		watch_atfork_child($self);
 		watch_imap_fetch_all($self, \@imap) if @imap;
 		watch_nntp_fetch_all($self, \@nntp) if @nntp;
diff --git a/lib/PublicInbox/Xapcmd.pm b/lib/PublicInbox/Xapcmd.pm
index 4e055acf..c2b66e69 100644
--- a/lib/PublicInbox/Xapcmd.pm
+++ b/lib/PublicInbox/Xapcmd.pm
@@ -11,6 +11,7 @@ use PublicInbox::SearchIdx;
 use File::Temp 0.19 (); # ->newdir
 use File::Path qw(remove_tree);
 use POSIX qw(WNOHANG _exit);
+use PublicInbox::DS;
 
 # support testing with dev versions of Xapian which installs
 # commands with a version number suffix (e.g. "xapian-compact-1.5")
@@ -102,10 +103,8 @@ sub commit_changes ($$$$) {
 
 sub cb_spawn {
 	my ($cb, $args, $opt) = @_; # $cb = cpdb() or compact()
-	my $seed = rand(0xffffffff);
-	my $pid = fork // die "fork: $!";
+	my $pid = PublicInbox::DS::do_fork;
 	return $pid if $pid > 0;
-	srand($seed);
 	$SIG{__DIE__} = sub { warn @_; _exit(1) }; # don't jump up stack
 	$cb->($args, $opt);
 	_exit(0);

^ permalink raw reply related	[relevance 6%]

* [PATCH 00/30] autodie-ification and code simplifications
@ 2023-10-17 23:37  7% Eric Wong
  2023-10-17 23:37  6% ` [PATCH 14/30] ds: introduce and use do_fork helper Eric Wong
  0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2023-10-17 23:37 UTC (permalink / raw)
  To: meta

Noisy code is less pleasant to work on, so use autodie more and
a few more simplifications.  There's a couple of small bugfixes
discovered along the way, too.

Eric Wong (30):
  lei_mirror: start converting to autodie
  lei_mirror: autodie most `close' calls
  lei_mirror: use autodie for most `open' calls
  git: introduce read_all function
  import: use read_all to detect short reads
  lei_mirror: use read_all
  use read_all in more places to improve safety
  xap_helper*: use autodie in more places
  xap_helper: die more easily in both implementations
  xap_helper: simplify SIGTERM exit checks
  xap_helper: autodie for getsockopt
  xap_client: autodie for pipe and socketpair
  xt/git-http-backend: remove Net::HTTP usage
  ds: introduce and use do_fork helper
  ds: get rid of SetLoopTimeout
  cindex: drop some unused functions
  syscall: common $F_SETPIPE_SZ definition
  t/lei-up: additional diagnostics for match failures
  test_common: use autodie and read_all where possible
  test_common: only hide TCP port in messages
  test_common: use $cwdfh for every run_script command
  init: drop extraneous `+'
  init: use autodie to reduce distractions
  xt/mem-imapd-tls: remove unused/broken epoll imports
  xt/mem-imapd-tls: reduce FDs for lsof use
  lei: use autodie where appropriate
  lei_auth: update comments and use v5.12
  lei_config: drop redundant open check
  convert: use read_all to simplify error checks
  idx_stack: use autodie + read_all

 lib/PublicInbox/CidxLogP.pm       |   4 +-
 lib/PublicInbox/CodeSearchIdx.pm  |   5 --
 lib/PublicInbox/DS.pm             |  36 ++++----
 lib/PublicInbox/Daemon.pm         |  16 ++--
 lib/PublicInbox/EOFpipe.pm        |   6 +-
 lib/PublicInbox/Gcf2.pm           |   7 +-
 lib/PublicInbox/Git.pm            |  19 +++--
 lib/PublicInbox/IPC.pm            |  12 +--
 lib/PublicInbox/IdxStack.pm       |  20 ++---
 lib/PublicInbox/Import.pm         |   8 +-
 lib/PublicInbox/InboxWritable.pm  |   6 +-
 lib/PublicInbox/LEI.pm            |  48 +++++------
 lib/PublicInbox/LeiALE.pm         |  11 +--
 lib/PublicInbox/LeiAuth.pm        |   7 +-
 lib/PublicInbox/LeiBlob.pm        |   6 +-
 lib/PublicInbox/LeiConfig.pm      |   4 +-
 lib/PublicInbox/LeiMailSync.pm    |   5 +-
 lib/PublicInbox/LeiMirror.pm      | 131 ++++++++++++++----------------
 lib/PublicInbox/LeiSucks.pm       |   5 +-
 lib/PublicInbox/LeiXSearch.pm     |   2 +-
 lib/PublicInbox/MultiGit.pm       |   3 +-
 lib/PublicInbox/SearchIdxShard.pm |  14 ++--
 lib/PublicInbox/Syscall.pm        |  16 ++--
 lib/PublicInbox/TestCommon.pm     |  85 +++++++++----------
 lib/PublicInbox/ViewVCS.pm        |  12 ++-
 lib/PublicInbox/WWW.pm            |   4 +-
 lib/PublicInbox/Watch.pm          |  11 +--
 lib/PublicInbox/XapClient.pm      |  11 +--
 lib/PublicInbox/XapHelper.pm      |  24 ++----
 lib/PublicInbox/XapHelperCxx.pm   |  11 +--
 lib/PublicInbox/Xapcmd.pm         |   5 +-
 lib/PublicInbox/xap_helper.h      |  60 ++++++--------
 script/public-inbox-convert       |   8 +-
 script/public-inbox-edit          |   4 +-
 script/public-inbox-init          |  30 +++----
 t/dir_idle.t                      |   2 +-
 t/ds-leak.t                       |   4 +-
 t/gcf2.t                          |   5 +-
 t/init.t                          |   7 ++
 t/lei-sigpipe.t                   |   7 +-
 t/lei-up.t                        |   4 +-
 xt/git-http-backend.t             |  30 +++----
 xt/mem-imapd-tls.t                |  21 ++---
 xt/mem-nntpd-tls.t                |   8 +-
 44 files changed, 335 insertions(+), 409 deletions(-)

^ permalink raw reply	[relevance 7%]

Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2023-10-17 23:37  7% [PATCH 00/30] autodie-ification and code simplifications Eric Wong
2023-10-17 23:37  6% ` [PATCH 14/30] ds: introduce and use do_fork helper Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).