kmarks2
kmarks2

Reputation: 4875

Simple Perl socket script hanging

I have a Perl script consisting of the following:

 #!/usr/bin/perl
 # Script name: socket.pl

 # Build socket:
 use IO::Socket;
 my $sock = new IO::Socket::INET (
                                  PeerAddr => '74.74.74.74',
                                  PeerPort => '1543',
                                  Proto => 'tcp',
                                 )
 or die "Could not create socket: $@\n"; 

 # Make a pipe-delimited string out of the four arguments:
 $data = "$ARGV[0]$ARGV[1]|$ARGV[2]|$ARGV[3]|$ARGV[4]";

 # Write to socket and immediately close:
 print $sock "$data";
 close($sock);

The problem starts to surface when the script gets called many times. In a scenario where the script is getting called 60 times per minute, if I pgrep for socket.pl, I see 100 or 200 instances that have been "running" for up to 3 or 4 minutes. The server to which they are writing appears to receive $data immediately and on an exception free socket.

Given that I don't see any ostensible errors on the peer, why is this script lingering around even though it appears to have written to the socket? Could the interpreter be bogging down or hitting some constraint or limit (the script is running on a very capable box so its not system limitations)?

Thanks.

Upvotes: 0

Views: 684

Answers (1)

Dmitry Mina
Dmitry Mina

Reputation: 3842

Not sure if this will help, but you could try closing the socket using the IO::Socket::INET itself: $sock->close() or $sock->shutdown(2). Also try adding exit(0) at the end.

Upvotes: 2

Related Questions