D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4344 - Sockets with multiple threads report missing/failed WSAStartup
Summary: Sockets with multiple threads report missing/failed WSAStartup
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Windows
: P2 normal
Assignee: Masahiro Nakagawa
URL:
Keywords:
: 4975 5183 (view as issue list)
Depends on:
Blocks:
 
Reported: 2010-06-18 21:02 UTC by Adrian Matoga
Modified: 2010-11-08 05:18 UTC (History)
7 users (show)

See Also:


Attachments
source for server and client to reproduce this issue (1.38 KB, application/x-zip-compressed)
2010-06-18 21:02 UTC, Adrian Matoga
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Adrian Matoga 2010-06-18 21:02:30 UTC
Created attachment 666 [details]
source for server and client to reproduce this issue

Hello,

I encountered a strange problem dealing with sockets and threads under Windows
(I use 64-bit 7 Pro).
The server socket successfully accepts a connection and passes obtained socket
to newly created thread, which deals with it correctly.
However, the server socket then breaks with the exception on any next
operation.
The SocketException.errorCode is always 10093, which stands for missing or
failed WSAStartup. Indeed, when I place WSAStartup after accepting connection,
it works well (but most probably needs matching WSACleanup() call).
The single-threaded version also presents no problems.
I tried with DMD 2.046 and 2.047 with same result.
I attach example sources of both server and client showing the issue.
Comment 1 godmyoh 2010-07-10 21:17:09 UTC
I also encountered same problem on Winsows XP.

Sample:
----------------------------------------------------
import core.thread;
import std.socket;

void main()
{
	auto thread = new Thread({});
	thread.start();
	thread.join();
	
	auto socket = new TcpSocket;
}
----------------------------------------------------

Results:
----------------------------------------------------
std.socket.SocketException: Unable to create socket
----------------------------------------------------

And I found following code in /trunk/phobos/std/socket.d.

193 static ~this() 
194 { 
195     version(Win32) 
196     { 
197         WSACleanup(); 
198     } 
199 } 
200 

I think that "shared static ~this()" is correct.
Comment 2 Rainer Schuetze 2010-08-10 01:34:13 UTC
After several hours of debugging I found the problem with the non-shared destructor aswell.

"shared static ~this" seems to do the trick for now, but when building a DLL, unloading it will also kill connections of the whole application. I'd say the calls to WSAStartup and WSACleanup should be explicite.

Fortunately, in my case, the DLL is never unloaded...
Comment 3 Steven Schveighoffer 2010-10-19 06:18:55 UTC
Fixed in changeset http://www.dsource.org/projects/phobos/changeset/2107

This does not fix the DLL issue, but phobos is not DLL friendly anyways.  If you wish, open a separate issue for that.
Comment 4 Stephan Dilly 2010-10-30 18:08:34 UTC
*** Issue 4975 has been marked as a duplicate of this issue. ***
Comment 5 Steven Schveighoffer 2010-11-08 05:18:37 UTC
*** Issue 5183 has been marked as a duplicate of this issue. ***