From nobody@FreeBSD.org  Fri May 13 19:17:54 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id AACBB1065670
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 13 May 2011 19:17:54 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id 9C5B08FC0C
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 13 May 2011 19:17:54 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p4DJHsBf027214
	for <freebsd-gnats-submit@FreeBSD.org>; Fri, 13 May 2011 19:17:54 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p4DJHsWV027213;
	Fri, 13 May 2011 19:17:54 GMT
	(envelope-from nobody)
Message-Id: <201105131917.p4DJHsWV027213@red.freebsd.org>
Date: Fri, 13 May 2011 19:17:54 GMT
From: Aragon Gouveia <aragon@phat.za.net>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Bizarre file descriptor race condition
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         157013
>Category:       bin
>Synopsis:       Bizarre file descriptor race condition
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    jilles
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri May 13 19:20:10 UTC 2011
>Closed-Date:    Fri May 13 23:31:06 UTC 2011
>Last-Modified:  Fri May 13 23:31:06 UTC 2011
>Originator:     Aragon Gouveia
>Release:        8.2-STABLE
>Organization:
>Environment:
FreeBSD igor.geek.sh 8.2-STABLE FreeBSD 8.2-STABLE #2: Thu May  5 14:52:46 SAST 2011     toor@igor.geek.sh:/usr/obj/usr/src-RELENG_8/sys/IGOR  amd64

>Description:
Please see the script pasted in the repeat field.

When it is run, the while loop exits after the first iteration.  However,
if the ssh command is commented, the loop executes two iterations.

Stranger still, on some of my FreeBSD systems the script works correctly
_sometimes_, but mostly not.


>How-To-Repeat:
#!/bin/sh

DFRTMP=$(mktemp -d /tmp/dfr.XXXXXX)
[ $? -eq 0 ] || exit 1

cat >${DFRTMP}/hosts <<"_EOF"
1 saturn.geek.sh
2 null
_EOF

while read hostid hostname; do
        echo ${hostid}:${hostname}
        ssh ${hostname} 'df -k'
done <${DFRTMP}/hosts

rm -rf ${DFRTMP}
>Fix:


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: jilles 
State-Changed-When: Fri May 13 23:18:18 UTC 2011 
State-Changed-Why:  
This is not a bug but a fairly common caveat with ssh(1). 

The problem is that ssh(1) does not know if the other side wants 
input and therefore it reads data and sends it to the other side. 
Once data has been read from a pipe there is no way to put it back. 
It would be possible with a regular file but determining the amount 
to put back would be very complicated and unreliable. 

If the other side is very fast, it might finish executing the 
command before ssh(1) attempts its first read and the script 
might work as intended. 

You can solve the problem by redirecting ssh's input (either with 
shell redirection or with the -n option) or by using a different 
file descriptor for the list, like: 

while read hostid hostname <&3; do 
echo ${hostid}:${hostname} 
ssh ${hostname} 'df -k' 
done 3<${DFRTMP}/hosts 


Responsible-Changed-From-To: freebsd-bugs->jilles 
Responsible-Changed-By: jilles 
Responsible-Changed-When: Fri May 13 23:18:18 UTC 2011 
Responsible-Changed-Why:  
Track. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=157013 
>Unformatted:
