Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

tango.net.Socket

Moderators: kris

Posted: 12/14/07 11:17:03 Modified: 03/09/08 13:37:44

Hi there,

I think there's a problem with Nethost.getHostByName (and probably also, other synchronized methods) From what is written in: here if I understand correctly method with synchronized attribute is synchronized only on per-class-instance basis. I suggest changing getHostByName from current:

    synchronized bool getHostByName(char[] name)
    {
        char[1024] tmp;

        hostent* he = gethostbyname(convert2C (name, tmp));

        if(!he)
            return false;
        validHostent(he);
        populate(he);
        return true;
    }

to:

    bool getHostByName(char[] name)
    {
        char[1024] tmp;
        hostent* he;

        synchronized {
            he = gethostbyname(convert2C (name, tmp));

            if(!he)
                return false;
            validHostent(he);
            populate(he);
        }
        return true;
    }

The same reffers to getHostByAddr.

If it's required I think, I could provide simple test-case for this issue.

Author Message

Posted: 02/27/08 18:21:10 -- Modified: 02/27/08 18:22:02 by
gim

Hi again,

since noone replied to this, and I see the code hasn't changed in svn, here is testcase: http://paste.dprogramming.com/dpf8jbb9

The programm on paste, should randomly crash with segmentation fault (at least on linux, I have no idea if windows has synchronous resolver), due to bad scope of synchronized as described in previous post.

Please run it a few times, or add more hostnames in order to crash :].

edit

Oh forgot to mention, that it doesn't crash after proposed fix.

Posted: 02/27/08 21:24:21

Hiya,

Thanks, gim. You're right, and that's a bad oversight on our part. it needs to be synchronized on a single global instance of something. If I recall correctly, we can do something like synchronized(Socket.class) {}

Posted: 02/28/08 04:15:49

SVN trunk should be ok now

Posted: 02/28/08 11:54:54

thanks! I think there can be also problem with another synchronized method toAddrString in IPv4Address (also Socket.d), since inet_ntoa doesn't have to be thread safe, but I must say I haven't tested how it behaves.