]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
lei: reuse FDs atfork and close explicitly
authorEric Wong <e@80x24.org>
Thu, 9 Nov 2023 10:09:36 +0000 (10:09 +0000)
committerEric Wong <e@80x24.org>
Thu, 9 Nov 2023 21:53:48 +0000 (21:53 +0000)
We'll avoid having a redundant STDERR FD open in lei workers,
and some explicit close() on `lei up' sockets reduces the
likelyhood of inadvertantly open FDs causing processes to
linger.

lib/PublicInbox/LEI.pm

index f32e5bbcbdef5fc7dc7cf34c6eb6270551732c68..681044c8dea9b0e62789a8808dd4f409f7f8055e 100644 (file)
@@ -574,19 +574,20 @@ sub _lei_atfork_child {
        my ($self, $persist) = @_;
        # we need to explicitly close things which are on stack
        my $cfg = $self->{cfg};
+       delete @$cfg{qw(-watches -lei_note_event)};
        if ($persist) {
                open $self->{3}, '<', '/';
                fchdir($self);
                close($_) for (grep(defined, delete @$self{qw(0 1 2 sock)}));
-               delete @$cfg{qw(-lei_store -watches -lei_note_event)};
+               delete $cfg->{-lei_store};
        } else { # worker, Net::NNTP (Net::Cmd) uses STDERR directly
-               open STDERR, '+>&='.fileno($self->{2});
+               open STDERR, '+>&', $self->{2};
                STDERR->autoflush(1);
+               $self->{2} = \*STDERR;
                POSIX::setpgid(0, $$) // die "setpgid(0, $$): $!";
-               delete @$cfg{qw(-watches -lei_note_event)};
        }
        close($_) for (grep(defined, delete @$self{qw(old_1 au_done)}));
-       delete $self->{-socks};
+       close($_) for (@{delete($self->{-socks}) // []});
        if (my $op_c = delete $self->{pkt_op_c}) {
                close(delete $op_c->{sock});
        }