user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 09/10] t/cmd_ipc: workaround signal handling raciness
Date: Thu, 25 Mar 2021 06:20:25 +0200	[thread overview]
Message-ID: <20210325042026.11270-10-e@80x24.org> (raw)
In-Reply-To: <20210325042026.11270-1-e@80x24.org>

Perl can't check for interrupts when inside a blocking syscall,
as there's no self-pipe mechanism inside Perl itself.  So fork
a child and have it repeated kill(2) instead of relying on alarm(3).
---
 t/cmd_ipc.t | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/t/cmd_ipc.t b/t/cmd_ipc.t
index 84f8fb4d..c5e715a1 100644
--- a/t/cmd_ipc.t
+++ b/t/cmd_ipc.t
@@ -10,7 +10,7 @@ pipe(my ($r, $w)) or BAIL_OUT;
 my ($send, $recv);
 require_ok 'PublicInbox::Spawn';
 my $SOCK_SEQPACKET = eval { Socket::SOCK_SEQPACKET() } // undef;
-use Time::HiRes qw(alarm);
+use Time::HiRes qw(usleep);
 
 my $do_test = sub { SKIP: {
 	my ($type, $flag, $desc) = @_;
@@ -53,13 +53,25 @@ my $do_test = sub { SKIP: {
 		is_deeply(\@fds, [ undef ], "EAGAIN $desc");
 		$s2->blocking(1);
 
-		my $alrm = 0;
-		local $SIG{ALRM} = sub { $alrm++ };
-		alarm(0.001);
-		@fds = $recv->($s2, $buf, length($src) + 1);
-		ok($!{EINTR}, "EINTR set by ($desc)");
-		is_deeply(\@fds, [ undef ], "EINTR $desc");
-		is($alrm, 1, 'SIGALRM hit');
+		if ($ENV{TEST_ALRM}) {
+			my $alrm = 0;
+			local $SIG{ALRM} = sub { $alrm++ };
+			my $tgt = $$;
+			my $pid = fork // xbail "fork: $!";
+			if ($pid == 0) {
+				# need to loop since Perl signals are racy
+				# (the interpreter doesn't self-pipe)
+				while (usleep(1000)) {
+					kill 'ALRM', $tgt;
+				}
+			}
+			@fds = $recv->($s2, $buf, length($src) + 1);
+			ok($!{EINTR}, "EINTR set by ($desc)");
+			kill('KILL', $pid);
+			waitpid($pid, 0);
+			is_deeply(\@fds, [ undef ], "EINTR $desc");
+			ok($alrm, 'SIGALRM hit');
+		}
 
 		close $s1;
 		@fds = $recv->($s2, $buf, length($src) + 1);

  parent reply	other threads:[~2021-03-25  4:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-25  4:20 [PATCH 00/10] lei testing improvements Eric Wong
2021-03-25  4:20 ` [PATCH 01/10] test_common: cleanup inbox objects after use Eric Wong
2021-03-25  4:20 ` [PATCH 02/10] lei: janky $PATH2CFG garbage collection Eric Wong
2021-03-25  4:20 ` [PATCH 03/10] test_common: TEST_LEI_ERR_LOUD does not hide path names Eric Wong
2021-03-25  4:20 ` [PATCH 04/10] lei add-external: do not initialize writable store Eric Wong
2021-03-25  4:20 ` [PATCH 05/10] lei_mirror: don't show success on failure Eric Wong
2021-03-25  4:20 ` [PATCH 06/10] t/*: drop unnecessary v1-specific index calls Eric Wong
2021-03-25  4:20 ` [PATCH 07/10] tests: "check-run" uses persistent lei daemon Eric Wong
2021-03-25  4:20 ` [PATCH 08/10] lei import: force store, improve test diagnostics Eric Wong
2021-03-25  4:20 ` Eric Wong [this message]
2021-03-25  4:20 ` [PATCH 10/10] t/lei: add more diagnostics for failures Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210325042026.11270-10-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCH 09/10] t/cmd_ipc: workaround signal handling raciness' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).