Hi all; first time here but I've been hacking Perl for many years (and UNIX for even longer). I have a super-bizarre problem that I hope some Perl or UNIX guru can advise me on.

I wrote some code back in 2007 that wants to find out the maximum # of bytes I can write to a pipe without it blocking (I want to know the pipe buffer size). So, I have this:

my ($rd, $wd, $sz); pipe($rd, $wd) or die "pipe(print): $!\n"; $sz = fpathconf($wd, _PC_PIPE_BUF) or die "fpathconf(pipe): $!\n";

Now, this has worked fine ever since. I'm running it on Red Hat EL 4 (Perl 5.8.5 32bit) and Red Hat EL 5 (Perl 5.8.8 64bit). Lately, for reasons I can't figure out, the second die() above is firing due to fpathconf failure, with an error code of "Bad file descriptor". This doesn't happen every time, and it's happening across different systems at different times, etc. Nothing in the above code has changed. I'm not aware of anything systematic that has changed on any of these systems but I don't have direct control over them.

I'm really stumped. I don't understand how the fpathconf() can fail with "Bad file descriptor", after the pipe() just succeeded in the previous line (the pipe() never fails that I've seen). I added code to print fileno($wd) and it was just "5" (nothing huge like I'm running out of FDs).

To try to harden the code I modified it so that if the fpathconf() fails on the write FD then I try it on the read FD. If that fails I choose a small value (512--on Linux this value is actually 4096) and continue rather than dying. I left some warning messages in for now so I could see if the problem is still happening. I've seen the fpathconf() on the write FD failing, then the fpathconf() on the read FD succeeds! I don't know (since the problem is intermittent and I have no way to reliably reproduce it) if this really fixes the problem or if I was just lucky. And, I have no idea WHY this would be the case

I'm hoping someone who knows more about the guts of this can give me some thoughts on how to handle it.


In reply to Why does fpathconf give "bad file descriptor"? by madscientist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.