Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 18 Feb 2005 16:18:41 +0300
From:      Deomid Ryabkov <myself@rojer.pp.ru>
To:        freebsd-hackers@freebsd.org
Subject:   occasional ECONNREFUSED when connect()ing.
Message-ID:  <4215EB31.8020107@rojer.pp.ru>

next in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
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 the output of the client as i see it:

----------------------------- sample output of x.pl 
-----------------------------
71590, http://localhost:8000/junk, 2000: starting.
59 500 Can't connect to localhost:8000 (Timeout connect: Connection refused)
127 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
213 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
259 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
363 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
402 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
416 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
690 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
703 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
754 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
947 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
960 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
997 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
1163 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
1272 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
1433 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
1612 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
1887 500 Can't connect to localhost:8000 (Timeout connect: Connection 
refused)
----------------------------- cut -----------------------------
(the confusing "Timeout connect:" message is a result of my 
IO/Socket/INET.pm hacking.
without it, just a confusing "Timeout" was reported. i added printing of 
actual error string.)

a fragment of output of the same script running under strace:

----------------------------- strace of x.pl -----------------------------
socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
fcntl(3, F_GETFL)                       = 0x2 (flags O_RDWR)
fcntl(3, F_GETFL)                       = 0x2 (flags O_RDWR)
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
fcntl(3, F_GETFL)                       = 0x2 (flags O_RDWR)
fcntl(3, F_SETFL, O_RDWR|O_NONBLOCK)    = 0
connect(3, {sa_family=AF_INET, sin_port=htons(8000), 
sin_addr=inet_addr("127.0.0.1")}, 16) = -1 ECONNREFUSED (Connection refused)
----------------------------- cut -----------------------------

and the source of both server and client used:

----------------------------- accepter.c (server) 
-----------------------------
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <stdio.h>

char *reply = "HTTP/1.0 200 OK\r\nConetnt-type: 
text/plain\r\n\r\nNothing special.\r\n";
int rlen;

int main(int argc, char **argv) {

        int i, cal;
        int s, ss, n;
        char *buf;
        struct sockaddr_in saddr, caddr;

        memset(&saddr, 0, sizeof saddr);
        saddr.sin_family = AF_INET;
        saddr.sin_addr.s_addr = htonl(INADDR_ANY);
        saddr.sin_port = htons(8000);

        ss = socket(PF_INET, SOCK_STREAM, 6);
        if (ss < 0) return 1;
        if (bind(ss, (struct sockaddr *) &saddr, sizeof saddr) < 0) 
return 2;
        if (listen(ss, 511) < 0) return 3;

        buf = (char *) malloc(4096);
        if (buf == NULL) return 4;

        rlen = strlen(reply);

        fprintf(stderr, "accepting.\n");

        for (cal = sizeof caddr;
             s = accept(ss, (struct sockaddr *) &caddr, &cal);
             cal = sizeof caddr) {

                n = read(s, buf, 4096);
                if (n > 0) {
                        write(s, reply, rlen);
                }
                close(s);
        }

        return (s < 0);
}
----------------------------- cut -----------------------------

----------------------------- x.pl (client) -----------------------------
#!/usr/bin/perl

$| = 1;

use LWP::UserAgent;

$url = 'http://localhost:8000/junk';

my $n = 2000;

print "$$, $url, $n: starting.\n";

for (my $i = 0; $i < $n; $i++) {
        my $ua = LWP::UserAgent->new;
        $ua->timeout(1000);
        $req = HTTP::Request->new("GET" => $url);
        $res = $ua->request($req);

        print $i." ".$res->status_line."\n" if (0 or $res->code == 500);
}

exit();
----------------------------- cut -----------------------------

--
Deomid Ryabkov aka Rojer
myself@rojer.pp.ru
rojer@sysadmins.ru
ICQ: 8025844


[-- Attachment #2 --]
0	*H
010	+0	*H
	#00U
v0
	*H
0b10	UZA1%0#U
Thawte Consulting (Pty) Ltd.1,0*U#Thawte Personal Freemail Issuing CA0
040331221809Z
050331221809Z0_10URyabkov10
U*Deomid10UDeomid Ryabkov1!0	*H
	myself@rojer.pp.ru0"0
	*H
0
x?ed:4y h"0$F;O 4{7k΢U-)A
{ ;qrk,6s+ZȀ&Qvi)ZRu%0 	@<r\=/)NK4풓d|$_&aNq!ڨ66.ʺ`WPFmd;,H,ؑPd0!J5SـdPIK3@]42g
򎼸\i3/0-0U0myself@rojer.pp.ru0U00
	*H
+~4YXf
DW?%\;҇B2n
Ϸ|Jճ2++fZ7[m~X㽀c6x*2%^LnmoUkQ|Z>?X\uɝp/:00U
v0
	*H
0b10	UZA1%0#U
Thawte Consulting (Pty) Ltd.1,0*U#Thawte Personal Freemail Issuing CA0
040331221809Z
050331221809Z0_10URyabkov10
U*Deomid10UDeomid Ryabkov1!0	*H
	myself@rojer.pp.ru0"0
	*H
0
x?ed:4y h"0$F;O 4{7k΢U-)A
{ ;qrk,6s+ZȀ&Qvi)ZRu%0 	@<r\=/)NK4풓d|$_&aNq!ڨ66.ʺ`WPFmd;,H,ؑPd0!J5SـdPIK3@]42g
򎼸\i3/0-0U0myself@rojer.pp.ru0U00
	*H
+~4YXf
DW?%\;҇B2n
Ϸ|Jճ2++fZ7[m~X㽀c6x*2%^LnmoUkQ|Z>?X\uɝp/:0?0
0
	*H
010	UZA10UWestern Cape10U	Cape Town10U
Thawte Consulting1(0&UCertification Services Division1$0"UThawte Personal Freemail CA1+0)	*H
	personal-freemail@thawte.com0
030717000000Z
130716235959Z0b10	UZA1%0#U
Thawte Consulting (Pty) Ltd.1,0*U#Thawte Personal Freemail Issuing CA00
	*H
0Ħ<UsUNʙZhup[v:aQP
0cZ,p+Z?qV˯<6$*+w=+>@dקe*TH<a@dr`00U00CU<0:08642http://crl.thawte.com/ThawtePersonalFreemailCA.crl0U0)U"0 010UPrivateLabel2-1380
	*H
HP.
fgCL!6-6/P p<ab:~t%Pb'qW%ݩ9 Oe_N4[5MwV!x!5$F]_eO1;070i0b10	UZA1%0#U
Thawte Consulting (Pty) Ltd.1,0*U#Thawte Personal Freemail Issuing CA
v0	+0	*H
	1	*H
0	*H
	1
050218131841Z0#	*H
	1'^d.3?/z2E@0R	*H
	1E0C0
*H
0*H
0
*H
@0+0
*H
(0x	+71k0i0b10	UZA1%0#U
Thawte Consulting (Pty) Ltd.1,0*U#Thawte Personal Freemail Issuing CA
v0z*H
	1ki0b10	UZA1%0#U
Thawte Consulting (Pty) Ltd.1,0*U#Thawte Personal Freemail Issuing CA
v0
	*H
RC2!|oQ9PegY0jzm$00SD$5#SE/Vy}wc%D-˭"&J2'gXXfv	(6ޭ̤LѶ'5϶3gkr*` }U*s1QHJ:p5<Ru6bEVMjNO*!t^R0>MBc/
mڙ驋'bsQ,~$xWnWZvj

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4215EB31.8020107>