Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread-safety of System.Net.WebSockets.ClientWebSocket

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?

like image 488
Mr.Wang from Next Door Avatar asked Oct 29 '22 23:10

Mr.Wang from Next Door


1 Answers

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.
like image 121
Nathan Schubkegel Avatar answered Dec 02 '22 11:12

Nathan Schubkegel