|Pathologically Eclectic Rubbish Lister|
Sharing sockets between the main script and threadby markseger (Beadle)
|on Nov 24, 2008 at 23:46 UTC||Need Help??|
markseger has asked for the
wisdom of the Perl Monks concerning the following question:
I have a thread that manages sockets by listening for connections and then opening/closing them as appropriate. I'm doing my actual communications in the main script by using the technique mentioned in several monk postings on how to share sockets, namely sharing the file number rather than the socket handle itself and doing an open in the main line using that number as in
open ($fd, ">&$fnum") or die;
$fd is now a descriptor that I can write to and it works like a champ. Sort of, and hence my question. I'm hoping I just have the open syntax wrong in which case the fix might be a simple one.
First I run the script and monitor the open sockets with netstat. Then I run a test script that simply connects to that socket and does a sysread, prints the results, closes the connection and exits.
Netstat shows the initial connection and then the socket goes into FIN_WAIT2 and hangs around for quite awhile or until I close my main script. On the other hand if I run the code non-threaded and do all the communications inline, I see the socket go to FIN_WAIT2 and then fairly quickly goes away.
My guess is that this is when the socket is closed by the test script, that close is detected and acknowledged by the thread where it was created, but copy of the socket that was created with the open() is not completing the communications handshake and I don't know enough about these mechanisms to know what to do.
Any help will be greatly appreciated.
btw - I also tried using the open technique in some non-threaded code and it too works correctly but leaves a connection in FIN_WAIT2 as well, so I'm guessing this has nothing to do with threads and more to do with having to close that second handle.