From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 9778D1FFA8 for ; Fri, 18 Dec 2020 12:09:52 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 20/26] lei: restore default __DIE__ handler for event loop Date: Fri, 18 Dec 2020 12:09:44 +0000 Message-Id: <20201218120950.23272-21-e@80x24.org> In-Reply-To: <20201218120950.23272-1-e@80x24.org> References: <20201218120950.23272-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: The kqueue code paths will trigger exceptions which are caught by eval{}, so we can't be calling exit() from the __DIE__ handler and expect eval to catch it. We only need the __DIE__ handler to deal with fork or open failures at startup (since stderr is pointed to /dev/null). After that we can rely on OnDestroy writing errors to syslog when it goes out of scope. --- lib/PublicInbox/LEI.pm | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 5399fade..95b48095 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -20,6 +20,7 @@ use PublicInbox::Syscall qw($SFD_NONBLOCK EPOLLIN EPOLLONESHOT); use PublicInbox::Sigfd; use PublicInbox::DS qw(now); use PublicInbox::Spawn qw(spawn); +use PublicInbox::OnDestroy; use Text::Wrap qw(wrap); use File::Path qw(mkpath); use File::Spec; @@ -386,7 +387,6 @@ sub optparse ($$$) { sub dispatch { my ($self, $cmd, @argv) = @_; local $SIG{__WARN__} = sub { err($self, "@_") }; - local $SIG{__DIE__} = 'DEFAULT'; return _help($self, 'no command given') unless defined($cmd); my $func = "lei_$cmd"; $func =~ tr/-/_/; @@ -602,12 +602,12 @@ sub lazy_start { my $oldset = PublicInbox::Sigfd::block_signals(); my $pid = fork // die "fork: $!"; return if $pid; + require PublicInbox::Listener; + require PublicInbox::EOFpipe; openlog($path, 'pid', 'user'); local $SIG{__DIE__} = sub { syslog('crit', "@_"); - exit $! if $!; - exit $? >> 8 if $? >> 8; - exit 255; + die; # calls the default __DIE__ handler }; local $SIG{__WARN__} = sub { syslog('warning', "@_") }; open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!\n"; @@ -616,10 +616,13 @@ sub lazy_start { setsid(); $pid = fork // die "fork: $!"; return if $pid; + $SIG{__DIE__} = 'DEFAULT'; + my $on_destroy = PublicInbox::OnDestroy->new(sub { + my ($owner_pid) = @_; + syslog('crit', "$@") if $@ && $$ == $owner_pid; + }, $$); $0 = "lei-daemon $path"; local %PATH2CFG; - require PublicInbox::Listener; - require PublicInbox::EOFpipe; $l->blocking(0); $eof_w->blocking(0); $eof_r->blocking(0); @@ -680,6 +683,7 @@ sub lazy_start { $n; # true: continue, false: stop }); PublicInbox::DS->EventLoop; + $@ = undef if $on_destroy; # quiet OnDestroy if we got here exit($exit_code // 0); }