in reply to Re^3: Trying to thread a daemon with threads and Thread::Queue
in thread Trying to thread a daemon with threads and Thread::Queue
Hmm. Have you managed to track down what code is executing when that silent abnormal termination occurs?Yep, with some extra logging..
Then added this in the handler thread:$SIG{PIPE} = sub { $log->warning("Caught SIGPIPE: $!"); $running = 1; + };
I simulate a connection from client:$log->warning("In thread: Attempting to shutdown handle associ +ated with fileno: $fno"); shutdown ($socket, 2) or $log->warning ("In thread: Shutdown e +rror: $!"); $log->warning("In thread: Attempting to close handle associate +d with fileno: $fno"); close $socket or $log->warning ("In thread: close error: $!"); $log->warning("In thread: Enqueing in Qclean fileno: $fno"); $Qclean->enqueue( $fno );
The connection is immediately closed as desired, but I get nothing back from the server for my request, when --according to the code in the thread-- I should be getting this back:$ perl -e 'printf "<request>getmyip</request>%c",0' | ./nc myhost.com +9999
The output in the log:<data><ip>x.x.x.x</ip></data>
Caused by the close() call after shutdown() in the handler thread (the code you previously suggested), not the close() in Main!August 29 09:12:44 xmlsockd-advanced[32652]: (1) : XML IP request from +: x.x.x.x August 29 09:12:44 xmlsockdx[32652]: In thread: Attempting to shutdown + handle associated with fileno: 5 August 29 09:12:44 xmlsockdx[32652]: In thread: Attempting to close ha +ndle associated with fileno: 5 August 29 09:12:44 xmlsockdx[32652]: Caught SIGPIPE: Broken pipe August 29 09:12:44 xmlsockdx[32652]: In thread: close error: Broken pi +pe
And in the main accept() loop I also remove the close() call, so it will look like this:$log->warning("In thread: Attempting to shutdown handle associated wit +h fileno: $fno"); shutdown ($socket, 2) or $log->warning ("In thread: shutdown error: $! +"); $log->warning("In thread: Enqueing in Qclean fileno: $fno"); $Qclean->enqueue( $fno );
Again, I simulate another connection from client via shell:while ( $Qclean->pending ) { my $fileno = $Qclean->dequeue(); delete $sockets{ $fileno }; }
I should've got this response:$ perl -e 'printf "<policy-file-request/>%c",0' | ./nc myhost.com 9999
But again, the connection is immediately closed on the client side before they even receive a response to their request as coded for in the handler thread..<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="* +" to-ports="9999" /></cross-domain-policy>
Probably as I said previously, having the shutdown on the dup handle in the thread causes SIGPIPE to be thrown because the main handle is still open in the main accept() thread. And for whatever reason (which I am unaware of), whatever the handler thread wrote back on the socket prior to the shutdown is lost and is not sent to the client. Hmmm???August 29 09:26:31 xmlsockd[7815]: (1) : XML IP request from: x.x.x.x August 29 09:26:31 xmlsockd[7815]: In thread: Attempting to shutdown h +andle associated with fileno: 5 August 29 09:26:31 xmlsockd[7815]: In thread: Enqueing in Qclean filen +o: 5 August 29 09:26:31 xmlsockd[7815]: Caught SIGPIPE: Broken pipe
In my tests (under win32), if I dont use shutdown in the hendler thread, the client connection stays open until the main thread gets around to closing its copy of the socket.Did you actually get a response back for your request when you have shutdown() in the handler thread?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Trying to thread a daemon with threads and Thread::Queue
by BrowserUk (Patriarch) on Aug 29, 2008 at 17:37 UTC |