Fixed static destructor so sockets aren't closed on every thread exit in windows. (see bug 4344 and 4951)

Fixed unlisted bug, there could be a race condition on linux with gethostbyname and gethostbyaddr, now synchronizing entire processing of hostent struct.
This commit is contained in:
Steven Schveighoffer 2010-10-19 13:15:30 +00:00
parent a3cabfb7e8
commit 6fb30a1b5e

View file

@ -191,7 +191,7 @@ shared static this()
}
static ~this()
shared static ~this()
{
version(Win32)
{
@ -535,12 +535,28 @@ class InternetHost
*/
bool getHostByName(string name)
{
hostent* he;
synchronized(this.classinfo) he = gethostbyname(toStringz(name));
version(Windows)
{
// TODO gethostbyname is deprecated in windows, use getaddrinfo
auto he = gethostbyname(toStringz(name));
if(!he)
return false;
validHostent(he);
populate(he);
}
else
{
// posix systems use global state for return value, so we
// must synchronize across all threads
synchronized(this.classinfo)
{
auto he = gethostbyname(toStringz(name));
if(!he)
return false;
validHostent(he);
populate(he);
}
}
return true;
}
@ -551,12 +567,28 @@ class InternetHost
bool getHostByAddr(uint addr)
{
uint x = htonl(addr);
hostent* he;
synchronized(this.classinfo) he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
version(Windows)
{
// TODO gethostbyaddr is deprecated in windows, use getnameinfo
auto he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
if(!he)
return false;
validHostent(he);
populate(he);
}
else
{
// posix systems use global state for return value, so we
// must synchronize across all threads
synchronized(this.classinfo)
{
auto he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
if(!he)
return false;
validHostent(he);
populate(he);
}
}
return true;
}
@ -569,12 +601,28 @@ class InternetHost
bool getHostByAddr(string addr)
{
uint x = inet_addr(std.string.toStringz(addr));
hostent* he;
synchronized(this.classinfo) he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
version(Windows)
{
// TODO gethostbyaddr is deprecated in windows, use getnameinfo
auto he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
if(!he)
return false;
validHostent(he);
populate(he);
}
else
{
// posix systems use global state for return value, so we
// must synchronize across all threads
synchronized(this.classinfo)
{
auto he = gethostbyaddr(&x, 4, cast(int)AddressFamily.INET);
if(!he)
return false;
validHostent(he);
populate(he);
}
}
return true;
}
}