in reply to socket select hangs after client restarts

I have no conclusions or solutions to offer, but an observation prompted by my own limited experience of using select. It might prompt others to authoritative answers.

You are not handling error conditions. The 4-arg select is defined as select RBITS,WBITS,EBITS,TIMEOUT where EBITS are those streams in your select group that have experienced error conditions.

My touchy-feely findings are that unless you handle those--using shutdown & close--you will be subject to a the default timeout which is often set to 900 seconds. Ie. 15 minutes. If, when your code freezes on you, you wait 15 minutes or so, you may find that it starts working again. Not a tenable situation, but it might help point to a fix. Or not!


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: socket select hangs after client restarts
by cdarke (Prior) on Feb 18, 2010 at 09:34 UTC

      Yes, I know, but I do not think it is the correct way to deal with the OPs problem. Better, I believe, to handle EBITS and shutdown & close streams with errors.

      But, as I've only done this stuff on Windows, and from the mention of fork I'm assuming the OP is on *nix, and WinSocks have a habit of behaving subtly (and not so subtly) differently, I'm not prepared to put any weight behind that assertion.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Heya - thanks for the responses! First, this is on Ubuntu Linux - sorry for not including that. Perl v5.10.0 I've update the code according to your suggestion:
        while (1) { # Set up bit vectors for polling my $rin = ''; my $ein = ''; my $rout; my $eout; vec ($rin, fileno ($sock) , 1) = 1; vec ($ein, fileno ($sock) , 1) = 1; foreach my $streamID (keys %openStreamsSock) { vec ($fin, fileno($openStreamsSock{$streamID}) , 1) = 1; vec ($ein, fileno($openStreamsSock{$streamID}) , 1) = 1; } # Wait for incoming message my $nfound = select ($rout=$rin, undef, $eout=$ein, 5); if ($nfound) { # Check client streams, close any in error foreach my $streamID (keys %openStreamsSock) { if (vec($eout, fileno($openStreamsSock{$streamID}),1)) { close ($openStreamsSock{$streamID}); delete ($openStreamsSock{$streamID}); } } if (vec($rout, fileno($sock),1)) { $openStreamsSock{$streamRequest++} = $sock->accept(); } else { foreach my $streamID (keys %openStreamsSock) { if (vec($rout, fileno($openStreamsSock{$streamID}),1)) { # read data off the socket; not a message here, just r +aw data $msgSize = sysread ($openStreamsSock{$streamID}, $msgReceived, 1048576); if (defined ($msgSize) && ($msgSize > 0)) { writeStreamData ($streamID, $msgReceived); } else { # $msgSize being 0 indicates end of stream, or # $msgSize being undef indicates error, so close close ($openStreamsSock{$streamID}); delete ($openStreamsSock{$streamID}); } } } } } else { print "$0: Normal timeout of select...\n"; } }
        Unfortunately, this doesn't seem to change the behavior in any way. I hope I'm using it correctly. What really gets me is that the timeout also stops functioning. It leads me to suspect the code got stuck somewhere else in the script. All indications from my debug statements (removed for readability) showed the script going all the way back to the select statement and then never coming back, but I can't be absolutely sure. If anyone else has ideas, I'm all ears. (uh, eyes)