develooper Front page | perl.perl5.porters | Postings from October 2017

[perl #132358] Implicit filehandle close not working when STDERRclosed

Thread Next
From:
Steve Hay
Date:
October 25, 2017 13:08
Subject:
[perl #132358] Implicit filehandle close not working when STDERRclosed
Message ID:
rt-4.0.24-9894-1508936916-878.132358-75-0@perl.org
# New Ticket Created by  Steve Hay 
# Please include the string:  [perl #132358]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=132358 >


This is a bug report for perl from steve.m.hay@googlemail.com,
generated with the help of perlbug 1.40 running under perl 5.27.6.


-----------------------------------------------------------------
[Please describe your issue here]

I have recently noticed temporary files being left behind by
ExtUtils::CBuilder's test suite, which I traced to the $SCRIPT
filehandle in ExtUtils::CBuilder::Platform::Windows::MSVC::write_compiler_script().

It should be automatically closed when that function exits, but it
isn't - causing the attempted later deletion of it to fail because of
a familiar Windows problem of not being able to delete an open file.

Furthermore, I found that the problem weirdly only occurs with the
changes made by https://perl5.git.perl.org/perl.git/commit/581ae9ba65
in place.

The closure of STDERR is the key thing in that change, and the problem
is easily reproduced as follows:

    use strict;
    use warnings;
    my $file = 'test.txt';
    close STDERR;
    {
      my $fh;
      if (open $fh, '>', $file) {
        print "Opened $file. Press <ENTER> to close and unlink it...\n";
        <STDIN>;
      }
      else {
        print "Open $file failed: $!";
        exit;
      }
    }
    if (unlink $file) {
      print "Unlinked $file\n";
    }
    else {
      print "Unlink $file failed: $!\n";
    }

On Windows, the $file does not get deleted at the end and the output is:

    Opened test.txt. Press <ENTER> to close and unlink it...
    Unlink test.txt failed: Permission denied

(Unfortunately, you probably won't see the problem as easily as that
on other OSes since they mostly don't have an issue deleting an open
file. But presumably the problem of $fh still being open should still
be evident by other means of inspection.)

If I simply comment out the "close STDERR;" line then the $file does
get deleted and the output is as expected:

    Opened test.txt. Press <ENTER> to close and unlink it...
    Unlinked test.txt

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=core
    severity=high
---
Site configuration information for perl 5.27.6:

Configured by shay at Wed Oct 25 13:40:39 2017.

Summary of my perl5 (revision 5 version 27 subversion 6) configuration:
  Commit id: 0cdc775ef423ad6415e6f80b9244c17a52bf5149
  Platform:
    osname=MSWin32
    osvers=6.3
    archname=MSWin32-x86-multi-thread
    uname=''
    config_args='undef'
    hint=recommended
    useposix=true
    d_sigaction=undef
    useithreads=define
    usemultiplicity=define
    use64bitint=undef
    use64bitall=undef
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
    bincompat5005=undef
  Compiler:
    cc='cl'
    ccflags ='-nologo -GF -W3 -O1 -MD -Zi -DNDEBUG -GL -DWIN32
-D_CONSOLE -DNO_STRICT -D_CRT_SECURE_NO_DEPRECATE
-D_CRT_NONSTDC_NO_DEPRECATE  -DPERL_TEXTMODE_SCRIPTS
-DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS'
    optimize='-O1 -MD -Zi -DNDEBUG -GL'
    cppflags='-DWIN32'
    ccversion='16.00.40219.01'
    gccversion=''
    gccosandvers=''
    intsize=4
    longsize=4
    ptrsize=4
    doublesize=8
    byteorder=1234
    doublekind=3
    d_longlong=undef
    longlongsize=8
    d_longdbl=define
    longdblsize=8
    longdblkind=0
    ivtype='long'
    ivsize=4
    nvtype='double'
    nvsize=8
    Off_t='__int64'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='link'
    ldflags ='-nologo -nodefaultlib -debug -opt:ref,icf -ltcg
-libpath:"c:\perl\lib\CORE"  -machine:x86'
    libpth="C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\\lib"
    libs=oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib
netapi32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib    version.lib
odbc32.lib odbccp32.lib comctl32.lib msvcrt.lib
    perllibs=oldnames.lib kernel32.lib user32.lib gdi32.lib
winspool.lib  comdlg32.lib advapi32.lib shell32.lib ole32.lib
oleaut32.lib    netapi32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib
version.lib odbc32.lib odbccp32.lib comctl32.lib msvcrt.lib
    libc=msvcrt.lib
    so=dll
    useshrplib=true
    libperl=perl527.lib
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_win32.xs
    dlext=dll
    d_dlsymun=undef
    ccdlflags=' '
    cccdlflags=' '
    lddlflags='-dll -nologo -nodefaultlib -debug -opt:ref,icf -ltcg
-libpath:"c:\perl\lib\CORE"  -machine:x86'


---
@INC for perl 5.27.6:
    C:/perl/site/lib
    C:/perl/lib

---
Environment for perl 5.27.6:
    HOME (unset)
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=C:\Windows\system32;C:\Windows;C:\Windows\system32\wbem;C:\perl\bin
    PERL_BADLANG (unset)
    SHELL (unset)

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About