Description
The program below reproduces the problem.
With 5.36.1 the final close() call returns 1 (success). The output is as expected:
open(): 1
print(): 1
<>: undef
close(): 1
With 5.38.0-RC2 it returns an empty string (failure). The output is:
open(): 1
print(): 1
<>: undef
close():
$!=Bad file descriptor, $^E=
It works (i.e. close returns 1) if I remove the deliberate failing attempt at reading from the write-only filehandle, so presumably this is somehow interfering with the return value of the close() call.
(If I insert a pause (e.g. a read on ) after the close() call then I find that it is possible to manually delete the file that was opened, which suggests that the close() operation has succeeded; it is just the return value from it that is wrong. By contrast, if I pause before the close() call then it is not possible to manually delete the file at that point, so hopefully that conclusion is correct.)
I have tested this with and without the recent CloseHandle() fix (commit 149c2e4), but I have the same result (failure) either way. In fact, it also fails with 5.37.9, which is prior to the original CloseHandle() change (commit 8552f09) so the problem must be something else.
Testing further, I've narrowed it down a bit more: This worked in 5.37.3, but fails in 5.37.4.
use strict;
use warnings;
my $file = 'test.txt';
my $ret = open my $fh, '>', $file;
print "open(): $ret\n";
$ret = print $fh "WTF\n";
print "print(): $ret\n";
seek $fh, 0, 0;
{
# Suppress "Filehandle $fh opened only for output" warning
no warnings 'io';
$ret = <$fh>;
print "<>: " . (defined $ret ? $ret : "undef") . "\n";
}
$ret = close $fh;
print "close(): $ret\n";
print "\$!=$!, \$^E=$^E\n" unless $ret;
This is on Windows, with Visual Studio 2022 v17.4. perl -V as follows (all default build options):
Summary of my perl5 (revision 5 version 38 subversion 0) configuration:
Platform:
osname=MSWin32
osvers=10.0.19045.3086
archname=MSWin32-x64-multi-thread
uname=''
config_args='undef'
hint=recommended
useposix=true
d_sigaction=undef
useithreads=define
usemultiplicity=define
use64bitint=define
use64bitall=undef
uselongdouble=undef
usemymalloc=n
default_inc_excludes_dot=define
Compiler:
cc='cl'
ccflags ='-nologo -GF -W3 -MD -DWIN32 -D_CONSOLE -DNO_STRICT -DWIN64 -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_WINSOCK_DEPRECATED_NO_WARNINGS -DPERL_TEXTMODE_SCRIPTS -DMULTIPLICITY -DPERL_IMPLICIT_SYS'
optimize='-O1 -Zi -GL -fp:precise'
cppflags='-DWIN32'
ccversion='19.34.31946'
gccversion=''
gccosandvers=''
intsize=4
longsize=4
ptrsize=8
doublesize=8
byteorder=12345678
doublekind=3
d_longlong=undef
longlongsize=8
d_longdbl=define
longdblsize=8
longdblkind=0
ivtype='__int64'
ivsize=8
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:"D:\Dev\Temp\perl\lib\CORE" -machine:AMD64 -subsystem:console,"5.02"'
libpth="C:\Program Files\Microsoft Visual Studio\2022 17.4\Professional\VC\Tools\MSVC\14.34.31933\lib\x64"
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 vcruntime.lib ucrt.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 vcruntime.lib ucrt.lib
libc=ucrt.lib
so=dll
useshrplib=true
libperl=perl538.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:"D:\Dev\Temp\perl\lib\CORE" -machine:AMD64 -subsystem:console,"5.02"'
Characteristics of this binary (from libperl):
Compile-time options:
HAS_LONG_DOUBLE
HAS_TIMES
HAVE_INTERP_INTERN
MULTIPLICITY
PERLIO_LAYERS
PERL_COPY_ON_WRITE
PERL_DONT_CREATE_GVSV
PERL_HASH_FUNC_SIPHASH13
PERL_HASH_USE_SBOX32
PERL_IMPLICIT_SYS
PERL_MALLOC_WRAP
PERL_OP_PARENT
PERL_PRESERVE_IVUV
PERL_USE_SAFE_PUTENV
USE_64_BIT_INT
USE_ITHREADS
USE_LARGE_FILES
USE_LOCALE
USE_LOCALE_COLLATE
USE_LOCALE_CTYPE
USE_LOCALE_NUMERIC
USE_LOCALE_TIME
USE_PERLIO
USE_PERL_ATOF
USE_THREAD_SAFE_LOCALE
Locally applied patches:
RC2
Built under MSWin32
Compiled at Jul 1 2023 11:53:13
@inc:
D:/Dev/Temp/perl/site/lib
D:/Dev/Temp/perl/lib