From nobody@FreeBSD.org  Tue Apr 20 14:01:50 2010
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 0ACCA106564A
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 20 Apr 2010 14:01:50 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id D514A8FC19
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 20 Apr 2010 14:01:49 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o3KE1nN1057133
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 20 Apr 2010 14:01:49 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o3KE1nBO057132;
	Tue, 20 Apr 2010 14:01:49 GMT
	(envelope-from nobody)
Message-Id: <201004201401.o3KE1nBO057132@www.freebsd.org>
Date: Tue, 20 Apr 2010 14:01:49 GMT
From: Lucius Windschuh <lwindschuh@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: script: Racy return value
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         145884
>Category:       bin
>Synopsis:       [patch] script(1): Racy return value
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Apr 20 14:10:03 UTC 2010
>Closed-Date:    Sat May 01 12:27:14 UTC 2010
>Last-Modified:  Sat May 01 12:27:14 UTC 2010
>Originator:     Lucius Windschuh
>Release:        9-CURRENT (i386)
>Organization:
>Environment:
FreeBSD t400 9.0-CURRENT FreeBSD 9.0-CURRENT #139 r206412MP: Fri Apr  9 11:14:32 CEST 2010     root@t400:/usr/obj/usr/src/sys/CURRENT  i386
>Description:
script -qa $some_file $some_cmd is supposed to return the same value that
the execution of $some_cmd gave (TODO: This is not mentioned in the man page).
Unfortunately, script's return value changes randomly between zero and the
expected value.

I am a bit uncertain if this is a kernel or userland bug.

As you see in the proposed patch, waiting for the child process to die
instead of simply looking for a dead child solves the issue. So either
the child is not dead when script tries to exit or the kernel has not
yet marked the child dead?

Another bit: "ktrace script -qa /tmp/foobar false" always returns the
right result: ktrace stops the race, as it seems.

Besides this, I was seeing this problem for quite a while (some months,
I think). This indicates that it was not introduced by a recent commit.

This bug made portupgrade hardly usable, as it did not reliably realize
that the build process has failed.
>How-To-Repeat:
Execute this command many times:
$ script -qa /tmp/foobar false && echo "This should not happen"

And sometimes, you see "This should not happen" which, well, should not
happen. :-)
>Fix:
See the attached file: Remove WNOHANG. I think that it does the right
thing as wait3 returns immediately if no child process exists, so that
finish() will return after the last child exited, which is exactly the
point at which script shall return.

Patch attached with submission follows:

Index: src/usr.bin/script/script.c
===================================================================
--- src/usr.bin/script/script.c	(revision 206560)
+++ src/usr.bin/script/script.c	(working copy)
@@ -223,7 +223,7 @@
 	int die, e, status;
 
 	die = e = 0;
-	while ((pid = wait3(&status, WNOHANG, 0)) > 0)
+	while ((pid = wait3(&status, 0, 0)) > 0)
 	        if (pid == child) {
 			die = 1;
 			if (WIFEXITED(status))


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: ed 
State-Changed-When: Sat May 1 12:27:13 UTC 2010 
State-Changed-Why:  
Fixed in r207453! 

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