is IO:Socket:INET thread safe?

Jesper Persson <[email protected]> Wed, 29 May 2013 08:16:57 +0200
Newsgroups gmane.comp.lang.perl.modules.lwp
Message-ID <CA+uC3V1X1hmk5Kpmwj0WvYviqx5Ze0A2BfONEvTxDowcb1Q8bA@mail.gmail.com>
--047d7b62447662424204ddd5553c
Content-Type: text/plain; charset=ISO-8859-1

Hi everybody,

I have a multithreaded program that monitors the content of web pages.
On a newly installed CentOS server I have seen strange behaviour where I
get 404 because http request is sent to a wrong ip address not maching the
domain that was requested.

I have inserted a print statement in IO:Socket:INET.pm in the procedure
_get_addr
to see what ip address the domain is translated to:

sub _get_addr {
    my($sock,$addr_str, $multi) = @_;
    my @addr;
    if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) {
        (undef, undef, undef, undef, @addr) = gethostbyname($addr_str);
    } else {
        my $h = inet_aton($addr_str);
        push(@addr, $h) if defined $h;
    }
    open(FH, ">>/tmp/socket_get_addr.log");
    print FH localtime().": addr_str: $addr_str, addr:
".inet_ntoa($addr[0])."\n";
    close(FH);
    @addr;
}

From the log file:
Wed May 29 07:38:09 2013: addr_str: dk1.siteimprove.com, addr: 93.160.60.57
Wed May 29 07:38:09 2013: addr_str: siteimprove.dk, addr: 93.160.60.57

the ip address of siteimprove.dk is wrong and at the same second just
before a lookup of dk1.siteimprove.com gets the same ip address and that is
correct.

7 minutes later in the log file the siteimprove.dk domain is looked up
correctly:
Wed May 29 07:45:11 2013: addr_str: siteimprove.dk, addr: 93.160.60.87

Manually looking up the ip on then server gets the right ip address.

this entry is in the hosts file:
93.160.60.87        siteimprove.dk

CentOS release 6.3 (Final)
Summary of my perl5 (revision 5 version 10 subversion 1) configuration:
IO:Socket:INET version: $VERSION = "1.31";

Any help would be greatly appreciated.

Regards
Jesper Persson

--047d7b62447662424204ddd5553c
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi everybody,<div><br></div><div style>I have a multithrea=
ded program that monitors the content of web pages.</div><div style>On a ne=
wly installed CentOS server I have seen strange behaviour where I get 404 b=
ecause http request is sent to a wrong ip address not maching the domain th=
at was requested.</div>

<div style><br></div><div style>I have inserted a print statement in IO:Soc=
ket:INET.pm in the procedure _get_addr</div><div style><div style>to see wh=
at ip address the domain is translated to:</div><div style><br></div><div>

sub _get_addr {</div><div>=A0 =A0 my($sock,$addr_str, $multi) =3D @_;</div>=
<div>=A0 =A0 my @addr;</div><div>=A0 =A0 if ($multi &amp;&amp; $addr_str !~=
 /^\d+(?:\.\d+){3}$/) {</div><div>=A0 =A0 =A0 =A0 (undef, undef, undef, und=
ef, @addr) =3D gethostbyname($addr_str);</div>

<div>=A0 =A0 } else {</div><div>=A0 =A0 =A0 =A0 my $h =3D inet_aton($addr_s=
tr);</div><div>=A0 =A0 =A0 =A0 push(@addr, $h) if defined $h;</div><div>=A0=
 =A0 }</div><div>=A0 =A0 open(FH, &quot;&gt;&gt;/tmp/socket_get_addr.log&qu=
ot;);</div><div>=A0 =A0 print FH localtime().&quot;: addr_str: $addr_str, a=
ddr: &quot;.inet_ntoa($addr[0]).&quot;\n&quot;;</div>

<div>=A0 =A0 close(FH);</div><div>=A0 =A0 @addr;</div><div>}</div><div><br>=
</div><div style>From the log file:</div><div style><div>Wed May 29 07:38:0=
9 2013: addr_str: <a href=3D"http://dk1.siteimprove.com">dk1.siteimprove.co=
m</a>, addr: 93.160.60.57</div>

<div>Wed May 29 07:38:09 2013: addr_str: <a href=3D"http://siteimprove.dk">=
siteimprove.dk</a>, addr: 93.160.60.57</div><div style><br></div><div style=
>the ip address of <a href=3D"http://siteimprove.dk">siteimprove.dk</a> is =
wrong and at the same second just before a lookup of <a href=3D"http://dk1.=
siteimprove.com">dk1.siteimprove.com</a> gets the same ip address and that =
is correct.</div>

<div style><br></div><div style>7 minutes later in the log file the <a href=
=3D"http://siteimprove.dk">siteimprove.dk</a> domain is looked up correctly=
:</div><div style>Wed May 29 07:45:11 2013: addr_str: <a href=3D"http://sit=
eimprove.dk">siteimprove.dk</a>, addr: 93.160.60.87<br>

</div><div style><br></div><div style>Manually looking up the ip on then se=
rver gets the right ip address.</div><div style><br></div><div style>this e=
ntry is in the hosts file:=A0</div><div style>93.160.60.87 =A0 =A0 =A0 =A0<=
a href=3D"http://siteimprove.dk">siteimprove.dk</a></div>

<div style><br></div><div style>CentOS release 6.3 (Final)<br></div><div st=
yle>Summary of my perl5 (revision 5 version 10 subversion 1) configuration:=
<br></div><div style>IO:Socket:INET version:=A0$VERSION =3D &quot;1.31&quot=
;;</div>

<div style><br></div><div style>Any help would be greatly appreciated.</div=
><div style><br></div><div style>Regards</div><div style>Jesper Persson</di=
v></div></div></div>

--047d7b62447662424204ddd5553c--