5

The document is unclear about the thread safety of the System.Net.WebSockets.ClientWebSocket class.

Is it safe to call the SendAsync method from multiple threads simultaneously?

Mr.Wang from Next Door
  • 13,670
  • 12
  • 64
  • 97
  • What is unclear about it? "Any public static (Shared in Visual Basic) members of this type are thread safe. **Any instance members are not guaranteed to be thread safe.**" Seems clear to me, so the answer to your question would be no, it does not seem to be safe. – Lasse V. Karlsen Jul 26 '16 at 06:27

1 Answers1

2

It's thread safe enough to get some work done.

According to the source (credit to Michael Kropat for discussing it at https://www.codetinkerer.com/2018/06/05/aspnet-core-websockets.html )

Thread-safety:

  • It’s acceptable to call ReceiveAsync and SendAsync in parallel. One of each may run concurrently.
  • It’s acceptable to have a pending ReceiveAsync while CloseOutputAsync or CloseAsync is called.
  • Attempting to invoke any other operations in parallel may corrupt the instance. Attempting to invoke a send operation while another is in progress or a receive operation while another is in progress will result in an exception.