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

bad file descriptor after failed read #11973

Open
p5pRT opened this issue Feb 26, 2012 · 6 comments
Open

bad file descriptor after failed read #11973

p5pRT opened this issue Feb 26, 2012 · 6 comments

Comments

@p5pRT
Copy link

p5pRT commented Feb 26, 2012

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

Searchable as RT111352$

@p5pRT
Copy link
Author

p5pRT commented Feb 26, 2012

From @ig3

Created by @ig3

I was surprised to find that after a failed read on a file descriptor
opened for write, many (but not all) operations on the file descriptor
failed with "bad file descriptor'.

What I expected was that the read would fail (because the file was
opened for write only) but subsequent operations (e.g. close, print,
seek, tell, etc.) would succeed.

Checking with Process Monitor (I saw this behavior on Windows 7), I
did not see any failed operations on the file at the Windows interface,
so I am guessing the bad file descriptor is from perl.

The context where this arose is a subroutine passed a file descriptor. The subroutine
was to determine whether seek and tell worked correctly with the given file
descriptor, so I wanted to read and write data if possible.

The attached test demonstrates the behavior.

Perl Info

Flags:
    category=core
    severity=low

Site configuration information for perl 5.12.3:

Configured by 1 at Sun May 15 16:53:01 2011.

Summary of my perl5 (revision 5 version 12 subversion 3) configuration:
   
  Platform:
    osname=MSWin32, osvers=5.1, archname=MSWin32-x86-multi-thread
    uname='Win32 strawberryperl 5.12.3.0 #1 Sun May 15 09:44:53 2011 i386'
    config_args='undef'
    hint=recommended, useposix=true, d_sigaction=undef
    useithreads=define, usemultiplicity=define
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=undef, use64bitall=undef, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='gcc', ccflags =' -s -O2 -DWIN32 -DHAVE_DES_FCRYPT  -DUSE_SITECUSTOMIZE -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-strict-aliasing -mms-bitfields -DPERL_MSVCRT_READFIX',
    optimize='-s -O2',
    cppflags='-DWIN32'
    ccversion='', gccversion='4.4.3', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='long long', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='g++', ldflags ='-s -L"C:\strawberry\perl\lib\CORE" -L"C:\strawberry\c\lib"'
    libpth=C:\strawberry\c\lib C:\strawberry\c\i686-w64-mingw32\lib
    libs=-lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32
    perllibs=-lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32
    libc=, so=dll, useshrplib=true, libperl=libperl512.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ', lddlflags='-mdll -s -L"C:\strawberry\perl\lib\CORE" -L"C:\strawberry\c\lib"'

Locally applied patches:
    


@INC for perl 5.12.3:
    C:/strawberry/perl/site/lib
    C:/strawberry/perl/vendor/lib
    C:/strawberry/perl/lib
    .


Environment for perl 5.12.3:
    HOME (unset)
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=C:\Program Files\Common Files\Microsoft Shared\Microsoft Online Services;C:\Program Files (x86)\Common Files\Microsoft Shared\Microsoft Online Services;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys\;C:\Program Files (x86)\Toshiba\Bluetooth Toshiba Stack\sys\x64\;C:\Program Files (x86)\Windows Live\Shared;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:\strawberry\perl\bin;C:\UnxUtils\usr\local\wbin;C:\Java\jdk1.7.0\bin;C:\GTK\bin;C:\Program Files (x86)\mpg123;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL
 Server\100\Tools\Binn\VSShell\Common7\IDE\;c:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files (x86)\Microsoft Visual Studio 9..0\Common7\IDE\PrivateAssemblies\
    PERL_BADLANG (unset)
    PERL_JSON_BACKEND=JSON::XS
    PERL_YAML_BACKEND=YAML
    SHELL=C:\strawberry\perl\site\bin\psh.bat

@p5pRT
Copy link
Author

p5pRT commented Feb 26, 2012

From @ig3

#!C​:/strawberry/perl/bin/perl.exe
#
use strict;
use warnings;

use Test​::More tests => 11;

my $file = 'test.txt';

ok(open(my $fh, '>', $file), "open $file for writing") or diag("$file​: $!");

ok(print($fh "test file"), "write some text to $file") or diag("$file​: $!");

{
  my $pos = tell($fh);
  cmp_ok($pos, '==', 9, "confirm position after write");
}

ok(seek($fh, 0, 0), "seek back to beginning of file") or diag("seek failed​: $!");

{
  my $pos = tell($fh);

  cmp_ok($pos, '==', 0, "confirm position after seek");
}

{
  my $count = read($fh, my $buf, 4);

  ok(!defined($count), "read should fail, returning undef");
}

{
  my $pos = tell($fh);

  cmp_ok($pos, '==', 0, "confirm position after read");
}

ok(print($fh "write more"), "write more text to $file after failed read") or diag("$file​: $!");

ok(close($fh), "close $file") or diag("$file​: $!");
cmp_ok(unlink($file), '==' , 1, "unlink $file") or diag("$file​: $!");
ok( ! -e $file, "$file is gone");

@p5pRT
Copy link
Author

p5pRT commented Feb 26, 2012

From @Leont

On Sun, Feb 26, 2012 at 7​:53 AM, ian.goodacre@​xtra.co.nz <
perlbug-followup@​perl.org> wrote​:

I was surprised to find that after a failed read on a file descriptor
opened for write, many (but not all) operations on the file descriptor
failed with "bad file descriptor'.

What I expected was that the read would fail (because the file was
opened for write only) but subsequent operations (e.g. close, print,
seek, tell, etc.) would succeed.

I have recently discovered the same issue, while finding an explanation for
bug #108124.

Checking with Process Monitor (I saw this behavior on Windows 7), I
did not see any failed operations on the file at the Windows interface,
so I am guessing the bad file descriptor is from perl.

That is correct too. I have a pretty good idea of how to fix, but I hadn't
gotten around to it yet. From what I understand it's too late to get into
5.16 though. I expect to have a fix ready for 5.17.0 though.

Leon

@p5pRT
Copy link
Author

p5pRT commented Feb 26, 2012

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

@p5pRT
Copy link
Author

p5pRT commented Feb 27, 2012

From @cpansprout

On Sun Feb 26 13​:44​:46 2012, LeonT wrote​:

I have recently discovered the same issue, while finding an
explanation for
bug #108124.

Should these be merged? My brain is not functional enough right now for
me to feel comfortable deciding that myself. :-)

--

Father Chrysostomos

@p5pRT
Copy link
Author

p5pRT commented Feb 27, 2012

From @Leont

On Mon, Feb 27, 2012 at 2​:33 AM, Father Chrysostomos via RT
<perlbug-followup@​perl.org> wrote​:

Should these be merged?  My brain is not functional enough right now for
me to feel comfortable deciding that myself. :-)

I don't think it should be, but the other bug should depend on this one.

Leon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants