]> Sergey Matveev's repositories - public-inbox.git/blob - t/ds-leak.t
No ext_urls
[public-inbox.git] / t / ds-leak.t
1 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
2 # Licensed the same as Danga::Socket (and Perl5)
3 # License: GPL-1.0+ or Artistic-1.0-Perl
4 #  <https://www.gnu.org/licenses/gpl-1.0.txt>
5 #  <https://dev.perl.org/licenses/artistic.html>
6 use strict; use v5.10.1; use PublicInbox::TestCommon;
7 use_ok 'PublicInbox::DS';
8
9 if ('close-on-exec for epoll and kqueue') {
10         use PublicInbox::Spawn qw(spawn);
11         my $pid;
12         my $evfd_re = qr/(?:kqueue|eventpoll)/i;
13
14         PublicInbox::DS->SetLoopTimeout(0);
15         PublicInbox::DS->SetPostLoopCallback(sub { 0 });
16
17         # make sure execve closes if we're using fork()
18         my ($r, $w);
19         pipe($r, $w) or die "pipe: $!";
20
21         PublicInbox::DS::add_timer(0, sub { $pid = spawn([qw(sleep 10)]) });
22         PublicInbox::DS::event_loop();
23         ok($pid, 'subprocess spawned');
24
25         # wait for execve, we need to ensure lsof sees sleep(1)
26         # and not the fork of this process:
27         close $w or die "close: $!";
28         my $l = <$r>;
29         is($l, undef, 'cloexec works and sleep(1) is running');
30
31         SKIP: {
32                 my $lsof = require_cmd('lsof', 1) or skip 'lsof missing', 1;
33                 my $rdr = { 2 => \(my $null) };
34                 my @of = grep(/$evfd_re/, xqx([$lsof, '-p', $pid], {}, $rdr));
35                 my $err = $?;
36                 skip "lsof broken ? (\$?=$err)", 1 if $err;
37                 is_deeply(\@of, [], 'no FDs leaked to subprocess');
38         };
39         if (defined $pid) {
40                 kill(9, $pid);
41                 waitpid($pid, 0);
42         }
43         PublicInbox::DS->Reset;
44 }
45
46 SKIP: {
47         require_mods('BSD::Resource', 1);
48         my $rlim = BSD::Resource::RLIMIT_NOFILE();
49         my ($n,undef) = BSD::Resource::getrlimit($rlim);
50
51         # FreeBSD 11.2 with 2GB RAM gives RLIMIT_NOFILE=57987!
52         if ($n > 1024 && !$ENV{TEST_EXPENSIVE}) {
53                 skip "RLIMIT_NOFILE=$n too big w/o TEST_EXPENSIVE for $0", 1;
54         }
55         my $cb = sub {};
56         for my $i (0..$n) {
57                 PublicInbox::DS->SetLoopTimeout(0);
58                 PublicInbox::DS->SetPostLoopCallback($cb);
59                 PublicInbox::DS::event_loop();
60                 PublicInbox::DS->Reset;
61         }
62         ok(1, "Reset works and doesn't hit RLIMIT_NOFILE ($n)");
63 };
64
65 done_testing;