Thread overview
[Issue 5183] New: WinSock error occurs when socket is created in thread other than main
Nov 07, 2010
Andrew Wiley
Nov 07, 2010
Andrew Wiley
Nov 07, 2010
Andrew Wiley
Nov 07, 2010
Stephan Dilly
Nov 07, 2010
Andrew Wiley
Nov 07, 2010
Andrew Wiley
November 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5183

           Summary: WinSock error occurs when socket is created in thread
                    other than main
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: debio264@gmail.com


--- Comment #0 from Andrew Wiley <debio264@gmail.com> 2010-11-06 18:29:48 PDT ---
I'm still investigating exactly what causes this, but here's a simple test case
to show what I'm talking about:
import std.stdio;
import std.socket;
import core.thread;

void main() {
    testSocket();
    auto t = new Thread(&testSocket);
    t.start();
}
shared ushort port = 5000;
public void testSocket() {
    try {
        auto socket = new TcpSocket();
        socket.bind(new InternetAddress("0.0.0.0", port++));

    }
    catch(SocketException e) {
        writefln("Error: %d", e.errorCode);
        return;
    }
    writefln("success!");
}


The output:
success!
Error: 10093

According to http://msdn.microsoft.com/en-us/library/ms740668(VS.85).aspx the
10093 code is WSANOTINITIALISED, which means the following:
Either the application has not called WSAStartup or WSAStartup failed. The
application may be accessing a socket that the current active task does not own
(that is, trying to share a socket between tasks), or WSACleanup has been
called too many times.

WSAStartup was definitely called because the first socket was created successfully. I'm still figuring out how the second one fails.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5183



--- Comment #1 from Andrew Wiley <debio264@gmail.com> 2010-11-06 18:35:01 PDT ---
Okay, after adding some debugging writeflns to std.socket, the output is now
this:
initializing WSA
Initializing socket
success!
cleaning up WSA
Initializing socket
Error: 10093
cleaning up WSA

So the problem is that the module destructor for std.socket is called when the first thread terminates, even though the second thread still needs it.

This is because the module constructor for std.socket is "shared static this()"
while the destructor is "static this()". Adding "shared" causes the code to run
successfully.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5183



--- Comment #2 from Andrew Wiley <debio264@gmail.com> 2010-11-06 18:38:34 PDT ---
Created an attachment (id=804)
Patch to correct the issue

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5183


Stephan Dilly <spam@extrawurst.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |spam@extrawurst.org
         Resolution|                            |INVALID


--- Comment #3 from Stephan Dilly <spam@extrawurst.org> 2010-11-06 18:51:56 PDT ---
this was already fixed in the latest release of dmd2.050

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5183



--- Comment #4 from Andrew Wiley <debio264@gmail.com> 2010-11-06 18:54:09 PDT ---
(In reply to comment #3)
> this was already fixed in the latest release of dmd2.050

Well, this is from an install of dmd2.050 on Windows, so something didn't get updated properly.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5183



--- Comment #5 from Andrew Wiley <debio264@gmail.com> 2010-11-06 18:57:27 PDT ---
(In reply to comment #4)
> (In reply to comment #3)
> > this was already fixed in the latest release of dmd2.050
> 
> Well, this is from an install of dmd2.050 on Windows, so something didn't get updated properly.

My apologies, I seem to have 2.049. Guess I installed the new version on the other machine I develop on.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 08, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5183


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@yahoo.com
         Resolution|INVALID                     |DUPLICATE


--- Comment #6 from Steven Schveighoffer <schveiguy@yahoo.com> 2010-11-08 05:18:37 PST ---
Properly marking this.  Bugs that are valid on some version of D that is not the latest version are not invalid, they can be duplicates or resolved (could have been a bug that was resolved but had no matching bug in bugzilla).

Thanks

*** This issue has been marked as a duplicate of issue 4344 ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------