Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Early termination of STDIN with combination of ssh and sleep

by orleans704 (Initiate)
on Dec 24, 2013 at 21:53 UTC ( #1068335=perlquestion: print w/replies, xml ) Need Help??

orleans704 has asked for the wisdom of the Perl Monks concerning the following question:

I’m stumped with a problem where STDIN gets terminated early only when fork’ed children use ssh and only when the parent sleeps between children. Below is a working stub. On 3 different SLES boxes (perl 5.10.0) it prints either 1 line, 2 lines, or 4096 bytes (terminating line 41 mid-line).

If I comment out the sleep OR remove the ssh (and just sleep), there is no problem and all 50 lines are processed. I don’t understand what the combination of ssh on the children and sleep of the parent have to do with preventing a full read of STDIN.

(The sleep is needed because without them I get several “ssh_exchange_identification: Connection closed by remote host” errors. The amount of sleep doesn’t appear to make any difference. The real code is ssh’ing to real nodes, localhost is just to show the error.)

Execute: ./p.pl < LIST

p.pl:

#!/usr/bin/perl use strict; use Time::HiRes qw(usleep); my $max = 0; while(my $prog = <STDIN>) { print STDERR "$prog"; $max++; die "Unable to fork\n" unless(defined(my $pid = fork)); if($pid == 0) # child { die "Unable to exec\n" unless(exec("/usr/bin/ssh localhost /bi +n/sleep 10")); # die "Unable to exec\n" unless(exec("/bin/sleep 10")); exit(0); } usleep(100000); } for(my $i = 0; $i < $max; $i++) { wait; } print STDERR "\nDONE\n";

LIST (each line is 99 chars + \n):

001 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 002 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 003 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 004 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 005 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 006 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 007 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 008 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 009 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 010 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 011 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 012 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 013 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 014 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 015 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 016 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 017 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 018 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 019 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 020 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 021 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 022 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 023 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 024 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 025 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 026 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 027 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 028 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 029 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 030 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 031 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 032 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 033 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 034 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 035 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 036 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 037 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 038 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 039 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 040 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 041 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 042 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 043 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 044 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 045 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 046 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 047 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 048 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 049 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 050 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345

strace (reads and writes line 2, finds no more STDIN, moves to wait statements):

… read(0, "002 1234567890123456789012345678"..., 4096) = 4096 write(2, "002 1234567890123456789012345678"..., 100002 123456789012345 +678901234567890123456789012345678901234567890123456789012345678901234 +56789012345 ) = 100 lseek(0, 200, SEEK_SET) = 200 lseek(0, 0, SEEK_CUR) = 200 clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIG +CHLD, child_tidptr=0x2aaaabb1c1d0) = 2120 nanosleep({0, 100000000}, NULL) = 0 read(0, "", 4096) = 0 wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 2117 …

Replies are listed 'Best First'.
Re: Early termination of STDIN with combination of ssh and sleep
by Crackers2 (Parson) on Dec 25, 2013 at 03:25 UTC

    Does it work if you change the exec to

    die "Unable to exec\n" unless(exec("/usr/bin/ssh localhost /bi +n/sleep 10 </dev/null"));

    or

    die "Unable to exec\n" unless(exec("/usr/bin/ssh -n localhost +/bin/sleep 10"));

    IIRC ssh may be pre-reading STDIN and then discarding the contents when it's not actually needed. Redirecting stdin for the ssh command makes sure there won't be any interference with your main program.

      Wow, that did the trick. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1068335]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-10-03 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (13 votes). Check out past polls.

    Notices?