Mailing List Archive

print() on closed filehandle w ith “Bad File Descri ptor”
I have a tcp server in perl (revision 5.0 version 8 subversion 0) which use this subroutine to log in a file.



sub logger {

return if ($LOGFILE eq "") ;

my ($idt, $str) = @_ ;

unless( defined($str) ) {

$str = $idt ;

$idt = '' ;

}

my ($s,$m,$h,$J,$M,$A) = localtime(time()) ;

if(!open(OUT,">>$LOGFILE"))

{

warn "logger:error open [$LOGFILE]:[$!]\n";

return;

}

if (!printf OUT "%4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d %s %s -> %s\n",

$A+1900, $M+1, $J, $h, $m, $s, $idt, $HOSTNAME, $str)

{

warn "logger:error print [$LOGFILE]:[$!]\n";

return;

}

if (!close(OUT))

{

warn "logger:error close [$LOGFILE]:[$!]\n";

return;

}

}



So there is simultaneous writing in this log file. Exceptionally , I have this error in STDERR:



printf() on closed filehandle

logger :error print [/my/path/logFile.LOG]:[Bad File Descriptor]



without any error on open!



How can I resolve this problem?
Thanks for your help!!!
Re: print() on closed filehandle with “Bad File Descriptor” [ In reply to ]
On Mon, May 7, 2012 at 1:41 AM, david b <davidb_tai@hotmail.com> wrote:
> I have a tcp server in perl (revision 5.0 version 8 subversion 0) which use
> this subroutine to log in a file.

Hi David,

This version of perl is almost a decade old, have you tried running it
on a version that is at least still supported by anyone? 5.8.0 was
notoriously buggy, and should probably not be used in production
anyway. And can you add the output of perl -V, so we know your
configuration. I can't reproduce this issue on 5.14.2 anyway.

> sub logger {
>         return if ($LOGFILE eq "") ;
>         my ($idt, $str) = @_ ;
>         unless( defined($str) ) {
>                 $str = $idt ;
>                 $idt = '' ;
>         }
>         my ($s,$m,$h,$J,$M,$A) = localtime(time()) ;
>         if(!open(OUT,">>$LOGFILE"))
>         {
>                 warn "logger:error open [$LOGFILE]:[$!]\n";
>                 return;
>         }
>         if (!printf OUT "%4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d %s %s -> %s\n",
>                       $A+1900, $M+1, $J, $h, $m, $s, $idt, $HOSTNAME, $str)
>         {
>                 warn "logger:error print [$LOGFILE]:[$!]\n";
>                 return;
>         }
>         if (!close(OUT))
>         {
>                 warn "logger:error close [$LOGFILE]:[$!]\n";
>                 return;
>         }
> }

That is neither a minimal nor a complete example. Could you post the
smallest program you can make that shows this issue?

Leon