You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
area-System.Net.SocketsdocumentationDocumentation bug or enhancement, does not impact product or test codequestionAnswer questions and provide assistance, not an issue with source code or documentation.
Are the Socket methods Send or SendTo thread-safe? i.e. can they be used in a re-entrant manner similar to HttpClient.SendAsync and co?
Our use is for StatsD, sending data over Ip or Udp, in order to to send multiple messages to the same server.
If yes it is thread-safe, then the docs don't mention that, and they should.
If not, then we could also use a fast thread-safe socket pool instead as per #26287
When issuing a blocking Winsock call such as sendto, Winsock may need to wait for a network event before the call can complete. Winsock performs an alertable wait in this situation, which can be interrupted by an asynchronous procedure call (APC) scheduled on the same thread. Issuing another blocking Winsock call inside an APC that interrupted an ongoing blocking Winsock call on the same thread will lead to undefined behavior, and must never be attempted by Winsock clients.
To be clear, we are talking about the class System.Net.Sockets.Socket
I have been shown some old docson that class that say "Thread Safety Instances of this class are thread safe."
Are the WinSock concerns due to the .NET Socket class wrapping this?
Is Winsock thread-safe?
On modern Windows stacks, yes, it is, within limits.
It is safe, for instance, to have one thread calling send() and another thread calling recv() on a single socket.
By contrast, it’s a bad idea for two threads to both be calling send() on a single socket. This is “thread-safe” in the limited sense that your program shouldn’t crash, and you certainly shouldn’t be able to crash the kernel, which is handling these send() calls. The fact that it is “safe” doesn’t answer key questions about the actual effect of doing this. Which call’s data goes out first on the connection? Does it get interleaved somehow? Don’t do this.
Applications that use some kind of non-synchronous socket typically have some I/O queue already. Of particular interest in this case is overlapped I/O or I/O completion ports, because these I/O strategies are also thread-friendly. You can tell Winsock about several OVERLAPPED blocks, and Winsock will finish sending one before it moves on to the next. This means you can keep a chain of these OVERLAPPED blocks, each perhaps added to the chain by a different thread. Each thread can also call WSASend() on the block they added, making your main loop simpler.
The c# socket class supports the old APM (async) model where the callback could be onto a different thread. Does this not suggest it will be threadsafe for multiple beginsend() on single socket anyway (as the callback would thread-hop anyway)?
If we believe to native (windows) docs, that @samsosa is right: Send:
When issuing a blocking Winsock call such as WSASend with the lpOverlapped parameter set to NULL, Winsock may need to wait for a network event before the call can complete. Winsock performs an alertable wait in this situation, which can be interrupted by an asynchronous procedure call (APC) scheduled on the same thread. Issuing another blocking Winsock call inside an APC that interrupted an ongoing blocking Winsock call on the same thread will lead to undefined behavior, and must never be attempted by Winsock clients.
The order of calls made to WSASend is also the order in which the buffers are transmitted to the transport layer. WSASend should not be called on the same stream-oriented socket concurrently from different threads, because some Winsock providers may split a large send request into multiple transmissions, and this may lead to unintended data interleaving from multiple concurrent send requests on the same stream-oriented socket.
Also, NetworkStream (simple wrapper over Socket) docs says that:
Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.
area-System.Net.SocketsdocumentationDocumentation bug or enhancement, does not impact product or test codequestionAnswer questions and provide assistance, not an issue with source code or documentation.
Activity
[-]Are Sockets thread Safe?[/-][+]Are Sockets thread-safe?[/+]karelz commentedon Jul 17, 2018
@geoffkizer @davidsh @wfurt do you know the answer from top of your head?
I have a feeling that they are not thread-safe.
davidsh commentedon Jul 17, 2018
cc: @stephentoub
samsosa commentedon Jul 17, 2018
From https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-sendto
omariom commentedon Jul 17, 2018
Looks like no
http://www.serverframework.com/asynchronousevents/2015/01/wsarecv-wsasend-and-thread-safety.html
AnthonySteele commentedon Jul 19, 2018
To be clear, we are talking about the class
System.Net.Sockets.SocketI have been shown some old docson that class that say "Thread Safety Instances of this class are thread safe."
Are the WinSock concerns due to the .NET
Socketclass wrapping this?samsosa commentedon Jul 19, 2018
Yes, the classic POSIX methods shall be thread-safe.
Indeed you can safely call all functions without crashing but this doesn't mean it will work flawless in any situation.
From http://tangentsoft.net/wskfaq/intermediate.html#threadsafety
OccasionalDJ commentedon Jul 19, 2018
but the next sentence says:
The c# socket class supports the old APM (async) model where the callback could be onto a different thread. Does this not suggest it will be threadsafe for multiple beginsend() on single socket anyway (as the callback would thread-hop anyway)?
https://github.com/dotnet/corefx/blob/8747830da748f18e29469ee7f895bc8107777ad6/src/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Windows.cs#L54
Can @geoffkizer or @stephentoub clarify this further?
karelz commentedon Sep 9, 2019
Triage: We need validation and then proper documentation.
[-]Are Sockets thread-safe?[/-][+]Are synchronous Sockets thread-safe?[/+]antonfirsov commentedon Jul 29, 2020
@geoffkizer isn't this kind of re-entrance that you are suggesting to remove as part of #24138 ? Or am I mixing up things?
geoffkizer commentedon Jul 30, 2020
No, #24138 is unrelated.
kirsan31 commentedon Oct 12, 2021
If we believe to native (windows) docs, that @samsosa is right:
Send:BeginSend/SendAsync:Also,
NetworkStream(simple wrapper overSocket) docs says that:wfurt commentedon Nov 30, 2022
triage: we should document current behavior. It seems like we have some gaps e.g. some operations are safe and some are not.