Search code examples
perlsockets

How to resolve or diagnose an issue where sysread() in a Perl script blocks on read indefinitely and unexpectedly?


I have some code that communicates with an ADAM module (Advantech's Ethernet I/O Modules, ADAM-6000/6200 series), it connects to a socket, and reads data successfully in a loop, and it used to work just fine but now it stops and waits on sysread() to finish and never finishes.

What can I do to maybe help resolve or diagnose this issue further?

Code

socket($socket, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die "Unable to Connect";
connect($socket, sockaddr_in('1000', inet_aton($ip_address)

....

print "\n\ndo_command() begin:\n";
print "Send: " . Data::Dumper::qquote($str) . "\n";

syswrite($socket, $str);
usleep(600000);
print "Wrote to socket\n";
print "Waiting on sysread to finish...\n";

my $bytes = sysread($socket, $data_line, 200);

print "Data Read: " . Data::Dumper::qquote($data_line) . "\n";
print "Bytes Read: $bytes\n";

Output

do_command() begin:
Send: "#01\r"
Wrote to socket
Waiting on sysread to finish...

Script execution does not proceed past the last line. After I restart the script it works again for a little bit and then stops again in the same place. Normally the script executes these instructions in a loop indefinitely.

strace output

write(4, "#01\r", 4)                    = 4
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=600000000}, NULL) = 0
write(1, "Wrote to socket\n", 16)       = 16
write(1, "Waiting on sysread to finish...\n", 32) = 32
read(4, ">+00.018+00.016+00.020+00.008-00"..., 200) = 58
write(1, "Data Read: \">+00.018+00.016+00.0"..., 73) = 73
write(1, "Bytes Read: 58\n", 15)        = 15
write(1, "\n", 1)                       = 1
write(1, "Sleeping for .1 seconds\n", 24) = 24
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=100000000}, NULL) = 0
write(1, "Read Cycle\n", 11)            = 11
write(1, "\n\ndo_command() begin:\n", 22) = 22
write(1, "Send: \"#01\\r\"\n", 14)      = 14
write(4, "#01\r", 4)                    = 4
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=0, tv_nsec=600000000}, NULL) = 0
write(1, "Wrote to socket\n", 16)       = 16
write(1, "Waiting on sysread to finish...\n", 32) = 32
read(4,

Solution

  • Rebooting remote machine resolved the issue.