From nobody@FreeBSD.org  Wed Nov  7 17:05:44 2001
Return-Path: <nobody@FreeBSD.org>
Received: from freefall.freebsd.org (freefall.FreeBSD.org [216.136.204.21])
	by hub.freebsd.org (Postfix) with ESMTP id 1BB3537B41B
	for <freebsd-gnats-submit@FreeBSD.org>; Wed,  7 Nov 2001 17:05:44 -0800 (PST)
Received: (from nobody@localhost)
	by freefall.freebsd.org (8.11.4/8.11.4) id fA815if28745;
	Wed, 7 Nov 2001 17:05:44 -0800 (PST)
	(envelope-from nobody)
Message-Id: <200111080105.fA815if28745@freefall.freebsd.org>
Date: Wed, 7 Nov 2001 17:05:44 -0800 (PST)
From: Alejandro Forero Cuervo <bachue@bachue.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: When killed Perl should flush output buffers
X-Send-Pr-Version: www-1.0

>Number:         31836
>Category:       bin
>Synopsis:       When killed Perl should flush output buffers
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Nov 07 17:10:01 PST 2001
>Closed-Date:    Sat Nov 10 09:20:40 PST 2001
>Last-Modified:  Sat Nov 10 09:24:29 PST 2001
>Originator:     Alejandro Forero Cuervo
>Release:        4.4-RELEASE
>Organization:
Freaks Unidos
>Environment:
FreeBSD hbzones.conelprofe.com 4.4-RELEASE FreeBSD 4.4-RELEASE #0: Tue Sep 18 11:57:08 PDT 2001     murray@builder.FreeBSD.org:/usr/src/sys/compile/GENERIC  i386

>Description:
When the Perl interpreter is killed with SIGINT or SIGTERM (and probably many
other trappable signals), it doesn't flush its output buffers.

By default, when someone calls Perl's print function, it performs unbuffered
writes.  When a Perl script exits, Perl does the right thing: it flushes the
output buffers.  However, when the Perl interpreter is killed, it doesn't flush
them.

This is confusing to many users (I just helped one in the spanish users'
mailing list) and forces them to call the autoflush method from IO::Handle or
set $| to 1 (to disable buffering) when all they really want is just to have
their streams flushed.

This doesn't affect GNU/Linux (Debian using Perl 5.6.0).  However, I have
tested it in OpenBSD and (unsurprisingly) it is also affected.

The Perl interpreter should catch most (all?  Hmm, what happens with SIGSEGV or
SIGBUS?  Hmm) trapable signals and automagically flush output buffers when they
are about to cause program termination.

I'm not sure if this is the right place to report this problem.  If I'm doing
something wrong, please forgive me and let me know so, in my efforts to help
FreeBSD, I no longer do it in the future.

>How-To-Repeat:
The following Perl script should illustrate well what happens:

    for ($i = 0; $i ne 10; $i ++) {
      sleep(1);
      print "!";
    }

When the user runs it, it exits after 10 seconds and the Perl interpreter
flushes the output buffer so the user sees "!!!!!!!!!!".

However, if the user interrupts the script (by, for instance, pressing C-c),
Perl does not flush the output buffer and the user sees nothing.

>Fix:
This doesn't fix the problem but is what the users have to do to get around it:
add
    $|=1;
at the beginning or their scripts or use IO::Handle's autoflush method.

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: tobez 
State-Changed-When: Sat Nov 10 09:20:40 PST 2001 
State-Changed-Why:  
> When the Perl interpreter is killed with SIGINT or SIGTERM (and 
> probably many other trappable signals), it doesn't flush its output 
> buffers. 

It should not.  If you want this functionality, set your own signal 
handlers ($SIG{INT} = sub { ... }; $SIG{TERM} = sub { ... };).  It is 
not the job of the interpreter to do this.  Perl, just like C, uses 
stdio, which is buffered by default.  In C, just like in Perl, you will 
have to set your own signal handlers if you want to flush the buffers on 
a signal. 


http://www.FreeBSD.org/cgi/query-pr.cgi?pr=31836 
>Unformatted:
