From owner-freebsd-hackers@FreeBSD.ORG Fri Feb 18 14:38:36 2005 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 1AEF016A4CE for ; Fri, 18 Feb 2005 14:38:36 +0000 (GMT) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.203]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9B5B443D3F for ; Fri, 18 Feb 2005 14:38:35 +0000 (GMT) (envelope-from peadar.edwards@gmail.com) Received: by wproxy.gmail.com with SMTP id 69so473447wri for ; Fri, 18 Feb 2005 06:38:35 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=Il72PZHsMRxuCq/TkwBhNKRpRzfMGKOsGiPaK8n/6tNuZAaAwGGBNnTquEriXbyeKlo/w+17N8Xc9F6Ai3JqwzX+8pqJyBziNMhsaAmsA2OXILcvIl+Ptwr2+GE+NvVEkGOsgPqXhTQJCG49ckfc0nRBLw9plXVtF0APg3tt5yc= Received: by 10.54.28.78 with SMTP id b78mr21907wrb; Fri, 18 Feb 2005 06:38:35 -0800 (PST) Received: by 10.54.57.20 with HTTP; Fri, 18 Feb 2005 06:38:35 -0800 (PST) Message-ID: <34cb7c8405021806386fb874ee@mail.gmail.com> Date: Fri, 18 Feb 2005 14:38:35 +0000 From: Peter Edwards To: Deomid Ryabkov In-Reply-To: <4215EB31.8020107@rojer.pp.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <4215EB31.8020107@rojer.pp.ru> X-Mailman-Approved-At: Sat, 19 Feb 2005 13:26:58 +0000 cc: freebsd-hackers@freebsd.org Subject: Re: occasional ECONNREFUSED when connect()ing. X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Peter Edwards List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 18 Feb 2005 14:38:36 -0000 On Fri, 18 Feb 2005 16:18:41 +0300, Deomid Ryabkov wrote: > I have a strange case of occasional refused connect()'s. > The system is running 4.10-STABLE. > > This was originally reported by one of our developers. > The test script he provided is a simple Perl script that repeatedly > fetches an URL > from Apache running on this same server. > In the run of 2000 iterations, there are 5-10 random errors fetching the > URL. > After further investigation and digging, I uncovered the true cause of > the error: > connect() returning ECONNREFUSED. > To see if this was somehow related to Apache, I wrote my own simple > server that > accept()'s a connection, read()'s what supposed to be request and > write()'s a stereotypic reply, > thus resembling an HTTP request/reply conversation. > The number of sporadically occuring connect() errors has increased somewhat, > maybe due to quicker turnaround of my stub-server. > The question is - why do connections get refused at all? > I can think of no valid reasons... Here's one: In your stub server, what do you set the backlog to in the call to listen(2)? By the time you get the file descriptor for the new call from accept(), the kernel has already established the TCP/IP connection, and is maintaining state (ie, using resources) for it If you take a while dealing with this new call, (starting a thread, forking, or even processing the connection), then by the time you invoke accept() again, any number of clients may have attempted to connect to you, and the kernel needs to maintain state for every connection that has been requested. The backlog on the listen call indicates how many of these connections the kernel should hold on to: If the number of unaccept()ed connections reaches the backlog, you'll start to get ECONREFUSED errors at the client. If your stub server is single-threaded, this'll be pretty much guaranteed to happen if the number of clients exceeds the backlog on ths server's listening socket. HTH, peadar.