Well you did not tell us the OS. First I would look at the conditions to get EBADF error conditions with select. Probably you got some error condition that invalidate read or write on the "latest selected desc".

Then you seem to be using a mixed scheme with an accept-fork loop and a select loop. usually I tend to use one or the other i.e

  • accept+fork server and deal with the request in each child, need to take care of communication back to the father (pre-forked schemes are useful too especially in conjunction with DBI).
  • polling server (select loop) you add a correct "accept"ed desc to your set of valid desc and let the OS poll for you. if you don't trust your clients it's probably better to go non-blocking which means you need to handle partial reads. To do that correctly you need something like Lincoln Stein's IO::SessionData (part of SOAP::Lite), IO::Multiplex is useful too. If the OS is linux some even loop based on epoll could be a nice way to handle things. There is a module based on libevent.
  • there are threading models too, but I don't use that in production (yet)
  • So I don't understand your grand scheme of things...can you explain a bit more. In any case the "sharing" of data between father and child you seem to imply seems suspicious to me: it can only be a "one-go" thing anyway, why not do the necessary init in the child anyway?

    One basic rule is also that given a desc opened (with the close-on-exec flag not set) in the father, only the child or the father must use it at any time, meaning better to close it in the father if meant for the child. Are you sure your code comply with that? Can you post a representative snippet we could try? If you use a file instead of a db, does it work?

    You could also have a look to Net::Server.

    cheers --stephan

    In reply to Re: Mysterious bad file descriptor after fork by sgt
    in thread Mysterious bad file descriptor after fork by gnork

    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.