RE: is IO:Socket:INET thread safe?

Francesco Nidito <[email protected]> Wed, 29 May 2013 01:12:56 -0700
Newsgroups gmane.comp.lang.perl.modules.lwp
Message-ID <-7619453328071328011@unknownmsgid>
--089e013c6a700484f904ddd6f3c7
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

  Unfortunately, gethostbyname is not reentrant.

(maybe a stupid question) Is there any particular reason why you cannot use
multiple processes instead?

Typed on a very small keyboard...
 ------------------------------
From: Jesper Persson <[email protected]>
Sent: =E2=80=8E29/=E2=80=8E05/=E2=80=8E2013 07:17
To: libwww <[email protected]>
Subject: is IO:Socket:INET thread safe?

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) =3D @_;
    my @addr;
    if ($multi && $addr_str !~ /^\d+(?:\.\d+){3}$/) {
        (undef, undef, undef, undef, @addr) =3D gethostbyname($addr_str);
    } else {
        my $h =3D 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 =3D "1.31";

Any help would be greatly appreciated.

Regards
Jesper Persson

--089e013c6a700484f904ddd6f3c7
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html><head>
<meta content=3D"text/html; charset=3Dutf-8" http-equiv=3D"Content-Type"></=
head>
<body>
<div>
<div style=3D"FONT-SIZE:11pt;FONT-FAMILY:Calibri,sans-serif">Unfortunately,=
 gethostbyname is not reentrant. <br><br>(maybe a stupid question) Is there=
 any particular reason why you cannot use multiple processes instead?<br>
<br>Typed on a very small keyboard...</div></div>
<div dir=3D"ltr">
<hr>
<span style=3D"FONT-SIZE:11pt;FONT-FAMILY:Calibri,sans-serif;FONT-WEIGHT:bo=
ld">From: </span><span style=3D"FONT-SIZE:11pt;FONT-FAMILY:Calibri,sans-ser=
if"><a href=3D"mailto:[email protected]">Jesper Persson</a></span>=
<br>
<span style=3D"FONT-SIZE:11pt;FONT-FAMILY:Calibri,sans-serif;FONT-WEIGHT:bo=
ld">Sent: </span><span style=3D"FONT-SIZE:11pt;FONT-FAMILY:Calibri,sans-ser=
if">=E2=80=8E29/=E2=80=8E05/=E2=80=8E2013 07:17</span><br><span style=3D"FO=
NT-SIZE:11pt;FONT-FAMILY:Calibri,sans-serif;FONT-WEIGHT:bold">To: </span><s=
pan style=3D"FONT-SIZE:11pt;FONT-FAMILY:Calibri,sans-serif"><a href=3D"mail=
to:[email protected]">libwww</a></span><br>
<span style=3D"FONT-SIZE:11pt;FONT-FAMILY:Calibri,sans-serif;FONT-WEIGHT:bo=
ld">Subject: </span><span style=3D"FONT-SIZE:11pt;FONT-FAMILY:Calibri,sans-=
serif">is IO:Socket:INET thread safe?</span><br><br></div></body></html><di=
v dir=3D"ltr">
Hi everybody,<div><br></div><div style>I have a multithreaded program that =
monitors the content of web pages.</div><div style>On a newly installed Cen=
tOS server I have seen strange behaviour where I get 404 because http reque=
st is sent to a wrong ip address not maching the domain that 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>=C2=A0 =C2=A0 my($sock,$addr_str, $multi) =3D @_;=
</div><div>=C2=A0 =C2=A0 my @addr;</div><div>=C2=A0 =C2=A0 if ($multi &amp;=
&amp; $addr_str !~ /^\d+(?:\.\d+){3}$/) {</div><div>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 (undef, undef, undef, undef, @addr) =3D gethostbyname($addr_str);</d=
iv>


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


<div>=C2=A0 =C2=A0 close(FH);</div><div>=C2=A0 =C2=A0 @addr;</div><div>}</d=
iv><div><br></div><div style>From the log file:</div><div style><div>Wed Ma=
y 29 07:38:09 2013: addr_str: <a href=3D"http://dk1.siteimprove.com">dk1.si=
teimprove.com</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:=C2=A0</div><div style>93.160.60.87 =C2=A0 =C2=A0=
 =C2=A0 =C2=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:=C2=A0$VERSION =3D &quot;1.31&q=
uot;;</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>

--089e013c6a700484f904ddd6f3c7--