From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Cc: Eric Wong <e@yhbt.net>
Subject: [PATCH] EOFpipe: avoid uninitialized variables in lei tests
Date: Fri, 8 Nov 2024 12:06:46 +0000 [thread overview]
Message-ID: <20241108120646.2173509-1-e@80x24.org> (raw)
From: Eric Wong <e@yhbt.net>
EINTR can happen due to spurious wakeups and lead to
uninitialized variable warnings when comparing the result of
->do_read to a numeric value. Thus avoid EINTR by making the
pipe non-blocking on initialization.
Furthermore, our ->do_read API is overkill and not appropriate
for only detecting EOF on a pipe since it is designed for
parsing network protocols. Relying on level-triggering makes
more sense here, since we only want to detect EOF and don't have
to worry about event loop monopolization.
---
lib/PublicInbox/EOFpipe.pm | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/lib/PublicInbox/EOFpipe.pm b/lib/PublicInbox/EOFpipe.pm
index 77b699a2..3eaa0e4e 100644
--- a/lib/PublicInbox/EOFpipe.pm
+++ b/lib/PublicInbox/EOFpipe.pm
@@ -4,21 +4,29 @@
package PublicInbox::EOFpipe;
use v5.12;
use parent qw(PublicInbox::DS);
-use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT $F_SETPIPE_SZ);
+use PublicInbox::Syscall qw(EPOLLIN $F_SETPIPE_SZ);
+use Errno qw(EAGAIN);
sub new {
my (undef, $rd, @cb_args) = @_;
my $self = bless { cb_args => \@cb_args }, __PACKAGE__;
# 4096: page size
fcntl($rd, $F_SETPIPE_SZ, 4096) if $F_SETPIPE_SZ;
- $self->SUPER::new($rd, EPOLLIN|EPOLLONESHOT);
+ $rd->blocking(0); # avoid EINTR
+ $self->SUPER::new($rd, EPOLLIN); # level trigger for spurious wakeup
}
sub event_step {
my ($self) = @_;
- if ($self->do_read(my $buf, 1) == 0) { # auto-closed
+ my $r = sysread $self->{sock}, my $buf, 1;
+ if (!defined $r) {
+ warn "W: EOFpipe read: $!\n" if $! != EAGAIN;
+ } elsif ($r == 0) {
+ $self->close;
my ($cb, @args) = @{delete $self->{cb_args}};
$cb->(@args);
+ } else {
+ warn "BUG? EOFpipe read $r bytes, expected 0\n";
}
}
reply other threads:[~2024-11-08 12:06 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20241108120646.2173509-1-e@80x24.org \
--to=e@80x24.org \
--cc=e@yhbt.net \
--cc=meta@public-inbox.org \
/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
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).