* [PATCH 04/21] move all non-test @post_loop_do into named subs
2023-10-04 3:49 7% [PATCH 00/21] lei + IPC related stuff Eric Wong
@ 2023-10-04 3:49 6% ` Eric Wong
0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2023-10-04 3:49 UTC (permalink / raw)
To: meta
Compared to Danga::Socket, our @post_loop_do API is designed to
make it easier to avoid anonymous subs (and their potential for
leaks in buggy old versions of Perl).
---
lib/PublicInbox/Daemon.pm | 38 ++++++++++++++++--------------
lib/PublicInbox/LEI.pm | 49 ++++++++++++++++++++-------------------
lib/PublicInbox/Watch.pm | 4 +++-
3 files changed, 48 insertions(+), 43 deletions(-)
diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index e5755981..a4c99cca 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -353,31 +353,33 @@ EOF
bless { pid => $$, pid_file => \$pid_file }, __PACKAGE__;
}
+sub has_busy_clients { # post_loop_do CB
+ my ($state) = @_;
+ my $now = now();
+ my $n = PublicInbox::DS::close_non_busy();
+ if ($n) {
+ if ($state->{-w} < now()) {
+ warn "$$ quitting, $n client(s) left\n";
+ $state->{-w} = now() + 5;
+ }
+ unless (defined $state->{0}) {
+ $state->{0} = (split(/\s+/, $0))[0];
+ $state->{0} =~ s!\A.*?([^/]+)\z!$1!;
+ }
+ $0 = "$state->{0} quitting, $n client(s) left";
+ }
+ $n; # true: loop continues, false: loop breaks
+}
+
sub worker_quit { # $_[0] = signal name or number (unused)
# killing again terminates immediately:
exit unless @listeners;
$_->close foreach @listeners; # call PublicInbox::DS::close
@listeners = ();
- my $proc_name;
- my $warn = 0;
+
# drop idle connections and try to quit gracefully
- @PublicInbox::DS::post_loop_do = (sub {
- my $now = now();
- my $n = PublicInbox::DS::close_non_busy();
- if ($n) {
- if (($warn + 5) < now()) {
- warn "$$ quitting, $n client(s) left\n";
- $warn = now();
- }
- unless (defined $proc_name) {
- $proc_name = (split(/\s+/, $0))[0];
- $proc_name =~ s!\A.*?([^/]+)\z!$1!;
- }
- $0 = "$proc_name quitting, $n client(s) left";
- }
- $n; # true: loop continues, false: loop breaks
- });
+ @PublicInbox::DS::post_loop_do = (\&has_busy_clients, { -w => 0 })
}
sub reopen_logs {
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index afed84c1..74a7f5b9 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -1271,13 +1271,30 @@ sub dir_idle_handler ($) { # PublicInbox::DirIdle callback
}
}
-sub drop_all_stores () {
- for my $cfg (values %PATH2CFG) {
- my $sto = delete($cfg->{-lei_store}) // next;
- eval { $sto->wq_io_do('done') };
- warn "E: $@ (dropping store for $cfg->{-f})" if $@;
- $sto->wq_close;
+sub can_stay_alive { # PublicInbox::DS::post_loop_do cb
+ my ($path, $dev_ino_expect) = @_;
+ if (my @st = defined($$path) ? stat($$path) : ()) {
+ if ($dev_ino_expect ne pack('dd', $st[0], $st[1])) {
+ warn "$$path dev/ino changed, quitting\n";
+ $$path = undef;
+ }
+ } elsif (defined($$path)) { # ENOENT is common
+ warn "stat($$path): $!, quitting ...\n" if $! != ENOENT;
+ undef $$path;
+ $quit->();
}
+ return 1 if defined($$path);
+ my $n = PublicInbox::DS::close_non_busy() or do {
+ # drop stores only if no clients
+ for my $cfg (values %PATH2CFG) {
+ my $sto = delete($cfg->{-lei_store}) // next;
+ eval { $sto->wq_io_do('done') };
+ warn "E: $@ (dropping store for $cfg->{-f})" if $@;
+ $sto->wq_close;
+ }
+ };
+ # returns true: continue, false: stop
+ $n + scalar(keys(%$PublicInbox::DS::AWAIT_PIDS));
}
# lei(1) calls this when it can't connect
@@ -1354,24 +1371,8 @@ sub lazy_start {
dir_idle_handler($_[0]) if $_[0]->fullname ne $path;
});
$dir_idle->add_watches([$sock_dir]);
- local @PublicInbox::DS::post_loop_do = (sub {
- if (@st = defined($path) ? stat($path) : ()) {
- if ($dev_ino_expect ne pack('dd', $st[0], $st[1])) {
- warn "$path dev/ino changed, quitting\n";
- $path = undef;
- }
- } elsif (defined($path)) { # ENOENT is common
- warn "stat($path): $!, quitting ...\n" if $! != ENOENT;
- undef $path;
- $quit->();
- }
- return 1 if defined($path);
- my $n = PublicInbox::DS::close_non_busy() or
- drop_all_stores(); # drop stores only if no clients
- # returns true: continue, false: stop
- $n + scalar(keys(%$PublicInbox::DS::AWAIT_PIDS));
- });
-
+ local @PublicInbox::DS::post_loop_do = (\&can_stay_alive,
+ \$path, $dev_ino_expect);
# STDIN was redirected to /dev/null above, closing STDERR and
# STDOUT will cause the calling `lei' client process to finish
# reading the <$daemon> pipe.
diff --git a/lib/PublicInbox/Watch.pm b/lib/PublicInbox/Watch.pm
index cf0720e3..3426d4a7 100644
--- a/lib/PublicInbox/Watch.pm
+++ b/lib/PublicInbox/Watch.pm
@@ -533,6 +533,8 @@ sub watch_nntp_init ($$) {
}
}
+sub quit_inprogress { !$_[0]->quit_done } # post_loop_do CB
+
sub watch { # main entry point
my ($self, $sig) = @_;
my $first_sig;
@@ -545,7 +547,7 @@ sub watch { # main entry point
add_timer(0, \&poll_fetch_fork, $self, $intvl, $uris);
}
watch_fs_init($self) if $self->{mdre};
- local @PublicInbox::DS::post_loop_do = (sub { !$self->quit_done });
+ local @PublicInbox::DS::post_loop_do = (\&quit_inprogress, $self);
PublicInbox::DS::event_loop($first_sig); # calls ->event_step
_done_for_now($self);
}
^ permalink raw reply related [relevance 6%]
* [PATCH 00/21] lei + IPC related stuff
@ 2023-10-04 3:49 7% Eric Wong
2023-10-04 3:49 6% ` [PATCH 04/21] move all non-test @post_loop_do into named subs Eric Wong
0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2023-10-04 3:49 UTC (permalink / raw)
To: meta
More work coming to make internal IPC stuff simpler
and better layered for future enhancements
(FUSE, increase xap_helper usage internally, etc).
Eric Wong (21):
lei: drop stores explicitly at daemon shutdown
ds: hoist out close_non_busy
ds: don't pass FD map to post_loop_do callback
move all non-test @post_loop_do into named subs
lei: close DirIdle (inotify) early at daemon shutdown
input_pipe: {args} is never undefined
lei: do_env combines fchdir and local
lei: get rid of l2m_progress PktOp callback
t/lei_to_mail: modernize and document test
lei: reuse PublicInbox::Config::noop
lei: keep signals blocked on daemon shutdown
mbox_lock: retry on EINTR and use autodie
lock: retry on EINTR, improve error reporting
treewide: use PublicInbox::Lock->new
gcf2: use PublicInbox::Lock
spawn: use autodie and PublicInbox::Lock
xap_helper: retry flock on EINTR
XapHelper.pm: use EINTR-aware recv_cmd wrapper
spawn: drop checks for directory writability
lei: document and local-ize $OPT hashref
searchidx: fix redundant `in' in warning message
lib/PublicInbox/DS.pm | 17 +++--
lib/PublicInbox/Daemon.pm | 47 ++++++-------
lib/PublicInbox/DirIdle.pm | 12 +++-
lib/PublicInbox/Gcf2.pm | 8 ++-
lib/PublicInbox/IPC.pm | 4 +-
lib/PublicInbox/InputPipe.pm | 11 ++-
lib/PublicInbox/LEI.pm | 127 ++++++++++++++++------------------
lib/PublicInbox/LeiAuth.pm | 4 +-
lib/PublicInbox/LeiConfig.pm | 25 +++----
lib/PublicInbox/LeiConvert.pm | 7 +-
lib/PublicInbox/LeiInspect.pm | 28 ++++----
lib/PublicInbox/LeiLcat.pm | 17 +++--
lib/PublicInbox/LeiMirror.pm | 2 +-
lib/PublicInbox/LeiQuery.pm | 19 +++--
lib/PublicInbox/LeiTag.pm | 6 +-
lib/PublicInbox/LeiToMail.pm | 20 +++---
lib/PublicInbox/LeiXSearch.pm | 113 +++++++++++++-----------------
lib/PublicInbox/Lock.pm | 52 ++++++++------
lib/PublicInbox/MboxLock.pm | 49 ++++++-------
lib/PublicInbox/PktOp.pm | 15 ++--
lib/PublicInbox/SearchIdx.pm | 2 +-
lib/PublicInbox/Spawn.pm | 32 ++++-----
lib/PublicInbox/TestCommon.pm | 7 +-
lib/PublicInbox/Watch.pm | 4 +-
lib/PublicInbox/XapHelper.pm | 11 ++-
lib/PublicInbox/xap_helper.h | 6 +-
t/lei-tag.t | 3 +
t/lei_to_mail.t | 37 +++++-----
t/solver_git.t | 3 +-
t/v2mirror.t | 2 +-
30 files changed, 339 insertions(+), 351 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-04 3:49 7% [PATCH 00/21] lei + IPC related stuff Eric Wong
2023-10-04 3:49 6% ` [PATCH 04/21] move all non-test @post_loop_do into named subs 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).