I'm confident that the basic close-on-exec mechanism of $^F works on all Unix platforms because I remember writing a test for it, t/run/cloexec.t, which should be run when Perl is built. Did you build Perl from source and, if so, did the cloexec.t test pass? Which Perl version are you using and which OS? Are you able to run the cloexec.t test on your Perl?

In any case, even if you didn't explicitly set $^F, I expect the socket fd would be greater than two and so should be closed across an exec for the default value of $^F (see perlvar for details on $^F close-on-exec behaviour).

BTW, you can see how close-on-exec is implemented by searching for close-on-exec in the Perl C sources, for example from pp_sys.c:

#if defined(HAS_FCNTL) && defined(F_SETFD) fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exe +c */ #endif
How well this works on all Unices and in all environments, I cannot be certain of. For example, Linux open call cautions:
Additionally, use of this flag is essential in some multithreaded programs since using a separate fcntl(2) F_SETFD operation to set the FD_CLOEXEC flag does not suffice to avoid race conditions where one thread opens a file descriptor at the same time as another thread does a fork(2) plus execve(2).

Moreover, calling close on a socket closes your program's interface to the socket not the socket itself. It is up to the kernel to close the socket and it may be kept alive for a few minutes after you close it (see unix socket faq).

It is going to be tricky to figure out what is going on. A tool like lsof would be especially useful. Perhaps strace too. Along with a very small test program to investigate what is going on.

Update: as a workaround, instead of calling system, you could try calling fork, then explicitly close the socket, then call exec.


In reply to Re: close_on_exec in Perl : close socket opened in parent process when fork child , is not working by eyepopslikeamosquito
in thread close_on_exec in Perl : close socket opened in parent process when fork child , is not working by chinaxing

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.