Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Unable to flush stdout: Invalid argument

by bakiperl (Beadle)
on Jan 22, 2017 at 13:27 UTC ( #1180118=perlquestion: print w/replies, xml ) Need Help??

bakiperl has asked for the wisdom of the Perl Monks concerning the following question:

I have recently upgraded perl for windows from version 5.16 to version 5.24 and suddenly two of my cgi scripts started to produce the following error message:

Unable to flush stdout: Invalid argument?

I really have no idea where to look. Any help is appreciated.

Thank you.

Replies are listed 'Best First'.
Re: Unable to flush stdout: Invalid argument
by haukex (Archbishop) on Jan 22, 2017 at 13:52 UTC

    Hi bakiperl,

    Based on the error message, I think this has something to do with the implicit closing of filehandles that Perl does, in this case the closing of STDOUT when Perl exits (see #57512 and 8abddda39, which was part of v5.24). Unfortunately, that's all I've got at the moment: In your scripts, apparently Perl can't flush and/or close STDOUT.

    If you could boil down your code to a script that is as short as possible but still displays the problem (SSCCE), we might be able to better assist.

    -- Hauke D

Re: Unable to flush stdout: Invalid argument
by Dallaylaen (Chaplain) on Jan 23, 2017 at 14:22 UTC

    The error message is introduced in this commit, which may give a hint on reproduction.

    You'll need (1) and END {} block that outputs something and doesn't flush and (2) failure to flush STDOUT.

    I've been unable to do it so far. Maybe it happens when STDOUT is closed by webserver from the outside and END block prints something...

    UPDATE Oh, has already been pointed out.

Re: Unable to flush stdout: Invalid argument
by ww (Archbishop) on Jan 22, 2017 at 13:45 UTC

    We may be able to help... if you show us your ( SSCCE ) code.

     $anecdote ne $data

      The scripts are pretty big and I checked that every filehandle is being closed but at the top of the scripts I have the following code. Could that be the reason?

      BEGIN { use CGI::Carp qw(carpout); open(LOG, ">>/Logs/Report.log") && carpout(\*LOG); carpout('LOG'); }
        "The scripts are pretty big ..."

        That makes no sense in response to an implicit suggestion to read: The SSCCE: Short, Self-Contained, Correct (Compilable) Example.

        From your OP:

        "Unable to flush stdout: Invalid argument?"

        Find out what generates that message and track back from there.

        You might also consider modifying that code such that it produces a more useful message. Something like:

        • What argument does it refer to?
        • What line did it occur on?
        • What other relevant information is available?
        "open(LOG, ">>/Logs/Report.log") ..."

        Avoid using global package variables: that leads to action-at-a-distance and bugs that are hard to track down. Instead, use lexical variables in the smallest scope possible.

        Use meaningful names. Using common, globally scoped, package variables (e.g. FILE, LOG, IN, OUT), just exacerbates the problem and makes your debugging task all the more difficult.

        Read the open documentation, then rewrite that code more like this:

        open my $report_log_fh, '>>', $logfilename ...

        Not checking for I/O errors is also a problem. Either write your own; something like:

        ... or die "Can't open '$logfilename' for appending: $!";

        Or save yourself all that effort and let Perl do it for you with the autodie pragma.


        Read the CGI::Carp documentation:

        "The carpout() function requires one argument, a reference to an open filehandle for writing errors."

        Your argument, 'LOG', is a literal string.

        Does your script start with these two lines?

        use strict; use warnings;

        If not, add them. They may well highlight all sorts of problems you weren't aware of. Like autodie, strict and warnings are two more examples of Perl doing the work for you, if you just ask.

        Finally, kindly read "How do I post a question effectively?" and follow the instructions therein. We're happy to help but we're not psychic: if you provide the appropriate information, you'll usually get a quick response, that actually answers your question, rather than guesswork and requests for more information.

        — Ken

        > The scripts are pretty big

        Maybe try using a binary search debugging strategy?


        If you don't know which line in a 100 line program is buggy, you'd try to run the first 50 lines and skip the rest. If the problem show up, you'd know this first segment contains the bug. You'd next try to split this and run the first 25 lines and see if the problem is there and so on until you have figured out a short enough piece to look at

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Re: Unable to flush stdout: Invalid argument
by Anonymous Monk on Jan 24, 2017 at 05:36 UTC
    ctrl+f flush stdout...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1180118]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-03-30 02:02 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (73 votes). Check out past polls.