From b93c7a7efed33c68a2cf229d3086d4edec082149 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 12 Dec 2019 21:16:49 +0000 Subject: daemon: use DESTROY for unlinking --pid-file This gets rid of the last "END{}" block in our code and cleans up a (temporary) circular reference. Furthermore, ensure the cleanup code still works in all configurations by adding tests and testing both the -W1 (default, 1 worker) and -W0 (no workers) code paths. --- lib/PublicInbox/Daemon.pm | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm index c2c05b96..dd9e7780 100644 --- a/lib/PublicInbox/Daemon.pm +++ b/lib/PublicInbox/Daemon.pm @@ -28,10 +28,8 @@ my %pids; my %listener_names; # sockname => IO::Handle my %tls_opt; # scheme://sockname => args for IO::Socket::SSL->start_SSL my $reexec_pid; -my $cleanup; my ($uid, $gid); my ($default_cert, $default_key); -END { $cleanup->() if $cleanup }; my %KNOWN_TLS = ( 443 => 'https', 563 => 'nntps' ); my %KNOWN_STARTTLS = ( 119 => 'nntp' ); @@ -245,14 +243,11 @@ sub daemonize () { die "could not fork: $!\n" unless defined $pid; exit if $pid; } - if (defined $pid_file) { - write_pid($pid_file); - my $unlink_pid = $$; - $cleanup = sub { - $cleanup = undef; # avoid cyclic reference - unlink_pid_file_safe_ish($unlink_pid, $pid_file); - }; - } + return unless defined $pid_file; + + write_pid($pid_file); + # for ->DESTROY: + bless { pid => $$, pid_file => $pid_file }, __PACKAGE__; } sub worker_quit { # $_[0] = signal name or number (unused) @@ -631,16 +626,16 @@ sub daemon_loop ($$$$) { PublicInbox::DS->SetLoopTimeout(1000); } PublicInbox::DS->EventLoop; - $parent_pipe = undef; } - sub run ($$$;$) { my ($default, $refresh, $post_accept, $nntpd) = @_; daemon_prepare($default); my $af_default = $default =~ /:8080\z/ ? 'httpready' : undef; - daemonize(); + my $for_destroy = daemonize(); daemon_loop($refresh, $post_accept, $nntpd, $af_default); + PublicInbox::DS->Reset; + # ->DESTROY runs when $for_destroy goes out-of-scope } sub do_chown ($) { @@ -656,4 +651,8 @@ sub write_pid ($) { do_chown($path); } +sub DESTROY { + unlink_pid_file_safe_ish($_[0]->{pid}, $_[0]->{pid_file}); +} + 1; -- cgit v1.2.3-24-ge0c7