Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: close_on_exec in Perl : close socket opened in parent process when fork child , is not working

by eyepopslikeamosquito (Canon)
on Jan 17, 2013 at 11:18 UTC ( #1013760=note: print w/ replies, xml ) Need Help??


in reply to close_on_exec in Perl : close socket opened in parent process when fork child , is not working

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.


Comment on Re: close_on_exec in Perl : close socket opened in parent process when fork child , is not working
Select or Download Code
Re^2: close_on_exec in Perl : close socket opened in parent process when fork child , is not working
by chinaxing (Acolyte) on Jan 18, 2013 at 02:56 UTC

    this should be the mojolicious's problem. normal socket program is ok .

    foreaxmple, the following code, close_on_exec works.
    #!/bin/env perl use Modern::Perl; use IO::Socket; # or Socket; $^F=2; # server my $server_port = 4444; my $server = IO::Socket::INET->new( LocalPort => $server_port, Type => SOCK_STREAM, Reuse => 1, Listen => 10) or die "Couldn't be a tcp server on port $server_port:$!\n"; my $client; while($client = $server->accept()){ say 'client connected ...'; system('/home/admin/t.sh &>/dev/null &'); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1013760]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-12-28 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (181 votes), past polls