From nobody@FreeBSD.org  Mon Aug  1 12:05:18 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 8CAC01065672
	for <freebsd-gnats-submit@FreeBSD.org>; Mon,  1 Aug 2011 12:05:18 +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 7D1838FC20
	for <freebsd-gnats-submit@FreeBSD.org>; Mon,  1 Aug 2011 12:05:18 +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 p71C5Iaq092636
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 1 Aug 2011 12:05:18 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p71C5Ieo092633;
	Mon, 1 Aug 2011 12:05:18 GMT
	(envelope-from nobody)
Message-Id: <201108011205.p71C5Ieo092633@red.freebsd.org>
Date: Mon, 1 Aug 2011 12:05:18 GMT
From: Mathieu <sigsys@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: accidental busy-waiting loop in fetch(3)
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         159352
>Category:       bin
>Synopsis:       [libc] [patch] accidental busy-waiting loop in fetch(3)
>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:   Mon Aug 01 12:10:05 UTC 2011
>Closed-Date:    Mon Mar 10 15:50:17 CDT 2014
>Last-Modified:  Mon Mar 10 15:50:17 CDT 2014
>Originator:     Mathieu
>Release:        8.2-STABLE
>Organization:
>Environment:
8.2-STABLE FreeBSD 8.2-STABLE #0 r223938M
>Description:
Sometimes, fetch(1) will use 100% CPU time while downloading.  There's a loop that will retry I/O syscalls that returned EAGAIN immediately without blocking due to a small bug.

>How-To-Repeat:
Probably only noticeable on slow links.

>Fix:
Index: common.c
===================================================================
--- common.c	(revision 223938)
+++ common.c	(working copy)
@@ -429,12 +429,6 @@
 	ssize_t rlen, total;
 	int r;
 
-	if (fetchTimeout) {
-		FD_ZERO(&readfds);
-		gettimeofday(&timeout, NULL);
-		timeout.tv_sec += fetchTimeout;
-	}
-
 	total = 0;
 	while (len > 0) {
 		/*
@@ -474,6 +468,11 @@
 			return (-1);
 		}
 		// assert(rlen == FETCH_READ_WAIT);
+		if (fetchTimeout) {
+			FD_ZERO(&readfds);
+			gettimeofday(&timeout, NULL);
+			timeout.tv_sec += fetchTimeout;
+		}
 		while (fetchTimeout && !FD_ISSET(conn->sd, &readfds)) {
 			FD_SET(conn->sd, &readfds);
 			gettimeofday(&now, NULL);
@@ -576,14 +575,13 @@
 	ssize_t wlen, total;
 	int r;
 
-	if (fetchTimeout) {
-		FD_ZERO(&writefds);
-		gettimeofday(&timeout, NULL);
-		timeout.tv_sec += fetchTimeout;
-	}
-
 	total = 0;
 	while (iovcnt > 0) {
+		if (fetchTimeout) {
+			FD_ZERO(&writefds);
+			gettimeofday(&timeout, NULL);
+			timeout.tv_sec += fetchTimeout;
+		}
 		while (fetchTimeout && !FD_ISSET(conn->sd, &writefds)) {
 			FD_SET(conn->sd, &writefds);
 			gettimeofday(&now, NULL);


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: bdrewery 
State-Changed-When: Mon Mar 10 15:50:16 CDT 2014 
State-Changed-Why:  
fixed in r234837 

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