From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 85C111F55F for ; Fri, 15 Sep 2023 10:11:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1694772717; bh=hwKlK1sai621ZBQamCZj3W7FnAeOsWSys9M8QiHUYXY=; h=From:To:Subject:Date:From; b=W+fgr5mCvesTZU4fRGjWsY7fd96H07FLeUxZf2NIcPObU1EPlsT4Cx9JatNgJSUSJ UWK73c/S+34K7kG5Qm8ODbpvIN+wSXfBxMLbzHWurjZcSDuz9oDIOhSlPK2/jo2MUe p8ejan3KIzql8NFlEHPjnsdYmNe/77NIeFvCt244= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH] lei: ensure we run DESTROY|END at daemon exit w/ kqueue Date: Fri, 15 Sep 2023 10:11:57 +0000 Message-ID: <20230915101157.2022726-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: The fundamental difference which I originally missed when implementing kqueue EVFILT_SIGNAL support is that it does not consume signals like signalfd(2) does. In other words, with EVFILT_SIGNAL, it's possible for a single signal to be delivered twice if we unblock signals upon leaving the event loop as we do in lei. Note: Our DS->event_loop and Sigfd APIs can/should probably be changed to better accomodate EVFILT_SIGNAL differences from signalfd without sacrificing usability of either. This fixes the problem of leftover lei-ovv.dst*, lei_cfg-* and skv.* files in $TMPDIR at the end of test suite runs on *BSD when IO::KQueue is installed. --- lib/PublicInbox/LEI.pm | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 5fbb1211..c61ce76d 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -1318,6 +1318,9 @@ sub lazy_start { USR1 => \&noop, USR2 => \&noop, }; + # for EVFILT_SIGNAL and signalfd behavioral difference: + my @kq_ign = eval { require PublicInbox::DSKQXS } ? keys(%$sig) : (); + require PublicInbox::DirIdle; local $dir_idle = PublicInbox::DirIdle->new(sub { # just rely on wakeup to hit post_loop_do @@ -1356,13 +1359,22 @@ sub lazy_start { $current_lei ? err($current_lei, @_) : warn( strftime('%Y-%m-%dT%H:%M:%SZ', gmtime(time))," $$ ", @_); }; + local $SIG{PIPE} = 'IGNORE'; open STDERR, '>&STDIN' or die "redirect stderr failed: $!"; open STDOUT, '>&STDIN' or die "redirect stdout failed: $!"; # $daemon pipe to `lei' closed, main loop begins: eval { PublicInbox::DS::event_loop($sig, $oldset) }; warn "event loop error: $@\n" if $@; + + # EVFILT_SIGNAL will get a duplicate of all the signals it was sent + local @SIG{@kq_ign} = map 'IGNORE', @kq_ign; + PublicInbox::DS::sig_setmask($oldset) if @kq_ign; + # exit() may trigger waitpid via various DESTROY, ensure interruptible - PublicInbox::DS::sig_setmask($oldset); + local @SIG{TERM} = sub { exit(POSIX::SIGTERM + 128) }; + local @SIG{INT} = sub { exit(POSIX::SIGINT + 128) }; + local @SIG{QUIT} = sub { exit(POSIX::SIGQUIT + 128) }; + PublicInbox::DS::sig_setmask($oldset) if !@kq_ign; dump_and_clear_log(); exit($exit_code // 0); }