Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO::Handle's error() method does not accurately reflect read errors #12782

Open
p5pRT opened this issue Feb 17, 2013 · 5 comments
Open

IO::Handle's error() method does not accurately reflect read errors #12782

p5pRT opened this issue Feb 17, 2013 · 5 comments
Labels
Closable? We might be able to close this ticket, but we need to check with the reporter distro-All type-library

Comments

@p5pRT
Copy link

p5pRT commented Feb 17, 2013

Migrated from rt.perl.org#116805 (status was 'open')

Searchable as RT116805$

@p5pRT
Copy link
Author

p5pRT commented Feb 17, 2013

From tchrist@perl.com

The documentation for IO​::Handle reads​:

  $io->error
  Returns a true value if the given handle has experienced any errors
  since it was opened or since the last call to "clearerr", or if the
  handle is invalid. It only returns false for a valid handle with no
  outstanding errors.

However, this is not true. For example, a failed read on a given handle
can set errno to EAGAIN, yet fail to be noted by the error() method on that
handle. This is shown by the enclosed test script, which is designed to
trigger an error on the read. You can tell that there is a bug because
it emits​:

  IMPOSSIBLE CASE #1​: errno set but has_error clear

--tom

#!/usr/bin/env perl

use v5.6; # needed for handle autoviv

use strict;
use warnings;
use warnings qw(deprecated closure syntax);
use sigtrap; # first released in v5, already covered by use

use Carp; # first released in v5, already covered by use
use Fcntl; # first released in v5, already covered by use
use Errno; # first released in v5.5.4, already covered by use

# IO​::Handle first released in v5.3.7, but not autoloaded till v5.14!!
use if ($] < 5.014), "IO​::Handle";

main();
exit();

##########################################################################

sub main {
  local $SIG{__WARN__} = sub { confess "fatalized warning @​_" };
  STDOUT->autoflush(1);

  test_tty();
  print "\nThat's all, folks!\n";
}

##########################################################################

sub test_tty {
  print "\nTesting TTY\n\n";
  test_nonblocking_read("/dev/tty");
}

sub test_nonblocking_read {
  my($path_name) = @​_;

  my $path_fh = undef;

  sysopen($path_fh, $path_name, O_RDONLY | O_NONBLOCK)
  || confess "can't sysopen $path_name​: $!";

  print "opened $path_name read-only, non-blocking\n";
  binmode($path_fh) || confess "binmode failed";

  print "handle blocking status = ", fmt_bool($path_fh->blocking), "\n";
  confess "need nonblocking read" if $path_fh->blocking;

  print "Enter something, quickly​: ";

  my $line = readline($path_fh);
  if (defined $line) {
  print "\nThank you, line length was​: ", length($line), "\n";
  }
  else {
################################################################
# CRITICAL​: must seize errno IMMEDIATELY lest it be lost
################################################################
  my $errno = $! || 0;
  my $has_error = $path_fh->error;
  print "\nToo late! readline failed\n";

  if ($errno || $has_error) {

  if ($errno && !$has_error) {
  print "IMPOSSIBLE CASE #1​: errno set but has_error clear\n";
  }
  if ($has_error && !$errno) {
  print "IMPOSSIBLE CASE #2​: has_error set but errno clear\n";
  }

  print "errno was ", fmt_errno($errno), "\n";
  print "path_fh->error was ", fmt_bool($has_error), "\n";

  }
  else {
  print "no errno or error state, must mean got EOF on read\n";
  }

  }
  print "\n";

  if (close($path_fh)) {
  print "closed $path_name\n";
  } else {
  print "ERROR​: can't close $path_name​: $!\n";
  }

}

sub fmt_bool {
  my($bool) = @​_;
  return $bool ? "TRUE" : "FALSE";
}

sub fmt_undef {
  my($scalar) = @​_;
  return defined($scalar) ? $scalar : "UNDEF";
}

BEGIN {

  my @​_Errno2Sym;
  for my $errname ( grep /^E\p{Lu}+$/, @​Errno​::EXPORT_OK ) {
  my $errnum = do {
  no strict "refs";
  &{"Errno​::$errname"};
  };
  $_Errno2Sym[$errnum] = $errname;
  }

  sub fmt_errno {
  my $num = 0 + $_[0];
  my $strerror = do {
  local $! = $num;
  "$!";
  };
  my $esym = $_Errno2Sym[$num] || "unknown error number #$num";
  return "$esym ($num)​: $strerror";
  }

}

__END__

Testing TTY

opened /dev/tty read-only, non-blocking
handle blocking status = FALSE
Enter something, quickly​:
Too late! readline failed
IMPOSSIBLE CASE #1​: errno set but has_error clear
errno was EAGAIN (35)​: Resource temporarily unavailable
path_fh->error was FALSE

closed /dev/tty

That's all, folks!

=============

Summary of my perl5 (revision 5 version 16 subversion 0) configuration​:
 
  Platform​:
  osname=darwin, osvers=9.8.0, archname=darwin-2level
  uname='darwin tom-christiansens-macbook-pro-2.local 9.8.0 darwin kernel version 9.8.0​: wed jul 15 16​:55​:01 pdt 2009; root​:xnu-1228.15.4~1release_i386 i386 '
  config_args=''
  hint=recommended, useposix=true, d_sigaction=define
  useithreads=undef, usemultiplicity=undef
  useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
  use64bitint=undef, use64bitall=undef, uselongdouble=undef
  usemymalloc=n, bincompat5005=undef
  Compiler​:
  cc='cc', ccflags ='-fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -I/opt/local/include',
  optimize='-O3',
  cppflags='-fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -I/opt/local/include'
  ccversion='', gccversion='4.0.1 (Apple Inc. build 5493)', gccosandvers=''
  intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
  d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
  ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
  alignbytes=8, prototype=define
  Linker and Libraries​:
  ld='env MACOSX_DEPLOYMENT_TARGET=10.3 cc', ldflags =' -fstack-protector -L/usr/local/lib -L/opt/local/lib'
  libpth=/usr/local/lib /opt/local/lib /usr/lib
  libs=-lgdbm -ldbm -ldb -ldl -lm -lutil -lc
  perllibs=-ldl -lm -lutil -lc
  libc=, so=dylib, useshrplib=false, libperl=libperl.a
  gnulibc_version=''
  Dynamic Linking​:
  dlsrc=dl_dlopen.xs, dlext=bundle, d_dlsymun=undef, ccdlflags=' '
  cccdlflags=' ', lddlflags=' -bundle -undefined dynamic_lookup -L/usr/local/lib -L/opt/local/lib -fstack-protector'

Characteristics of this binary (from libperl)​:
  Compile-time options​: HAS_TIMES PERLIO_LAYERS PERL_DONT_CREATE_GVSV
  PERL_MALLOC_WRAP PERL_PRESERVE_IVUV USE_LARGE_FILES
  USE_LOCALE USE_LOCALE_COLLATE USE_LOCALE_CTYPE
  USE_LOCALE_NUMERIC USE_PERLIO USE_PERL_ATOF
  Built under darwin
  Compiled at Jul 15 2012 13​:29​:37
  %ENV​:
  PERL_UNICODE="S"
  @​INC​:
  /usr/local/lib/perl5/site_perl/5.16.0/darwin-2level
  /usr/local/lib/perl5/site_perl/5.16.0
  /usr/local/lib/perl5/5.16.0/darwin-2level
  /usr/local/lib/perl5/5.16.0
  /usr/local/lib/perl5/site_perl/5.14.0
  /usr/local/lib/perl5/site_perl/5.13.9
  /usr/local/lib/perl5/site_perl/5.13.8
  /usr/local/lib/perl5/site_perl/5.13.0
  /usr/local/lib/perl5/site_perl/5.12.3
  /usr/local/lib/perl5/site_perl/5.12.2
  /usr/local/lib/perl5/site_perl/5.11.3
  /usr/local/lib/perl5/site_perl/5.10.0
  /usr/local/lib/perl5/site_perl
  .

@p5pRT
Copy link
Author

p5pRT commented Feb 23, 2013

From @jkeenan

On Sun Feb 17 14​:53​:43 2013, tom christiansen wrote​:

The documentation for IO​::Handle reads​:

$io->error
Returns a true value if the given handle has experienced any
errors
since it was opened or since the last call to "clearerr", or if
the
handle is invalid. It only returns false for a valid handle with
no
outstanding errors.

However, this is not true. For example, a failed read on a given
handle
can set errno to EAGAIN, yet fail to be noted by the error() method on
that
handle. This is shown by the enclosed test script, which is designed
to
trigger an error on the read. You can tell that there is a bug
because
it emits​:

IMPOSSIBLE CASE \#1&#8203;: errno set but has\_error clear

--tom

Confirming that I got the same output on Darwin/PPC and Linux/i386.

jimk

@p5pRT
Copy link
Author

p5pRT commented Feb 23, 2013

The RT System itself - Status changed from 'new' to 'open'

@xenu xenu removed the Severity Low label Dec 29, 2021
@tonycoz tonycoz added the Closable? We might be able to close this ticket, but we need to check with the reporter label Apr 11, 2024
@tonycoz
Copy link
Contributor

tonycoz commented Apr 11, 2024

I believe this was fixed by 80c1f1e

@jkeenan
Copy link
Contributor

jkeenan commented Apr 14, 2024

I believe this was fixed by 80c1f1e

Here's what I got when I ran Tom's program with perl-5.38.0 today. (The server's Debian Linux has been upgraded a couple of times since 2013).

$ perl -v | head -2 | tail -1
This is perl 5, version 38, subversion 0 (v5.38.0) built for x86_64-linux
$ perl ./116805_iohandle_read.pl 

Testing TTY

opened /dev/tty read-only, non-blocking
handle blocking status = FALSE
Enter something, quickly: 
Too late! readline failed
errno was EWOULDBLOCK (11): Resource temporarily unavailable
path_fh->error was TRUE

ERROR: can't close /dev/tty: Resource temporarily unavailable

That's all, folks!

That's different from what Tom originally reported -- but I don't know whether it means the problem has been fixed or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closable? We might be able to close this ticket, but we need to check with the reporter distro-All type-library
Projects
None yet
Development

No branches or pull requests

4 participants