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 7A2A61FB09 for ; Thu, 21 Jan 2021 19:46:25 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 10/12] lei: remove @TO_CLOSE_ATFORK_CHILD Date: Thu, 21 Jan 2021 19:46:22 +0000 Message-Id: <20210121194624.32002-11-e@80x24.org> In-Reply-To: <20210121194624.32002-1-e@80x24.org> References: <20210121194624.32002-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: ..At least limit it to a single file handle. The write end EOFpipe can be limited in scope and auto-closed when $quit is clobbered, leaving only the listener. The listener is the only handle that needs to be closed explicitly due to it being on the stack in the Listener->event_step => accept_dispatch => lei_$FOO code path. Everything else gets clobbered by DS->Reset in children after forking. --- lib/PublicInbox/LEI.pm | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index ccfc1649..37b45a00 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -27,7 +27,7 @@ use Text::Wrap qw(wrap); use File::Path qw(mkpath); use File::Spec; our $quit = \&CORE::exit; -our ($current_lei, $errors_log); +our ($current_lei, $errors_log, $listener); my ($recv_cmd, $send_cmd); my $GLP = Getopt::Long::Parser->new; $GLP->configure(qw(gnu_getopt no_ignore_case auto_abbrev)); @@ -35,7 +35,6 @@ my $GLP_PASS = Getopt::Long::Parser->new; $GLP_PASS->configure(qw(gnu_getopt no_ignore_case auto_abbrev pass_through)); our %PATH2CFG; # persistent for socket daemon -our @TO_CLOSE_ATFORK_CHILD; # TBD: this is a documentation mechanism to show a subcommand # (may) pass options through to another command: @@ -281,8 +280,7 @@ sub fail ($$;$) { sub atfork_prepare_wq { my ($self, $wq) = @_; - my $tcafc = $wq->{-ipc_atfork_child_close} //= []; - push @$tcafc, @TO_CLOSE_ATFORK_CHILD; + my $tcafc = $wq->{-ipc_atfork_child_close} //= [ $listener // () ]; if (my $sock = $self->{sock}) { push @$tcafc, @$self{qw(0 1 2)}, $sock; } @@ -307,7 +305,6 @@ sub atfork_child_wq { %PATH2CFG = (); undef $errors_log; $quit = \&CORE::exit; - @TO_CLOSE_ATFORK_CHILD = (); (__WARN__ => sub { err($self, @_) }, PIPE => sub { $self->x_it(13); # SIGPIPE = 13 @@ -837,12 +834,12 @@ sub lazy_start { die "connect($path): $!"; } umask(077) // die("umask(077): $!"); - socket(my $l, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!"; - bind($l, pack_sockaddr_un($path)) or die "bind($path): $!"; - listen($l, 1024) or die "listen: $!"; + local $listener; + socket($listener, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!"; + bind($listener, pack_sockaddr_un($path)) or die "bind($path): $!"; + listen($listener, 1024) or die "listen: $!"; my @st = stat($path) or die "stat($path): $!"; my $dev_ino_expect = pack('dd', $st[0], $st[1]); # dev+ino - pipe(my ($eof_r, $eof_w)) or die "pipe: $!"; local $oldset = PublicInbox::DS::block_signals(); if ($narg == 5) { $send_cmd = PublicInbox::Spawn->can('send_cmd4'); @@ -869,20 +866,21 @@ sub lazy_start { return if $pid; $0 = "lei-daemon $path"; local %PATH2CFG; - local @TO_CLOSE_ATFORK_CHILD = ($l, $eof_w); - $l->blocking(0); - $l = PublicInbox::Listener->new($l, \&accept_dispatch, $l); + $listener->blocking(0); my $exit_code; - local $quit = sub { - $exit_code //= shift; - my $listener = $l or exit($exit_code); - # closing eof_w triggers \&noop wakeup - $eof_w = $l = $path = undef; - $listener->close; # DS::close - PublicInbox::DS->SetLoopTimeout(1000); + my $pil = PublicInbox::Listener->new($listener, \&accept_dispatch); + local $quit = do { + pipe(my ($eof_r, $eof_w)) or die "pipe: $!"; + PublicInbox::EOFpipe->new($eof_r, \&noop, undef); + sub { + $exit_code //= shift; + my $lis = $pil or exit($exit_code); + # closing eof_w triggers \&noop wakeup + $listener = $eof_w = $pil = $path = undef; + $lis->close; # DS::close + PublicInbox::DS->SetLoopTimeout(1000); + }; }; - PublicInbox::EOFpipe->new($eof_r, \&noop, undef); - undef $eof_r; my $sig = { CHLD => \&PublicInbox::DS::enqueue_reap, QUIT => $quit,