AcceptSocket does not support Thread.Abort request

I understand that Thread.Abort should raise a ThreadAbortException in a blocked thread, however this does not seem to be the case with TcpListener.AcceptSocket . Here is the most basic illustration of the problem:

 class Program { static void Main(string[] args) { Thread thread = new Thread(Listen); thread.Start(); Thread.Sleep(1000); // give it a second to get going Console.WriteLine("Aborting listener thread"); thread.Abort(); thread.Join(); Console.WriteLine("Listener thread finished press <enter> to end app."); Console.ReadLine(); } static void Listen() { try { Console.WriteLine("Starting to listen"); TcpListener listener = new TcpListener(IPAddress.Any, 4070); listener.Start(); Socket socket = listener.AcceptSocket(); Console.WriteLine("Connected!"); return; } catch (ThreadAbortException exception) { Console.WriteLine("Abort requested"); } } } 

A call to thread.Abort() should stop AcceptSocket and execute a ThreadAbortException handler. Howerver does not do this.

Switching my Listen AcceptSocket wrapper to ListenAsync, which calls BeginAcceptSocket instead:

 static void ListenAsync() { try { ManualResetEvent clientConnected = new ManualResetEvent(false); Console.WriteLine("Starting to listen"); TcpListener listener = new TcpListener(IPAddress.Any, 4070); listener.Start(); clientConnected.Reset(); var iasyncResult = listener.BeginAcceptSocket((ar) => { Socket socket = listener.EndAcceptSocket(ar); Console.WriteLine("Connected!"); clientConnected.Set(); }, null); clientConnected.WaitOne(); return; } catch (ThreadAbortException exception) { Console.WriteLine("Abort requested"); } } 

In this case, the "seems" is working fine. A ThreadAbortException is thrown when a thread executes WaitOne. HOWEVER, the thread created by calling BeginAcceptSocket is still working and is able to accept a socket (I checked this by opening this port using Telnet)

Finally, I added Listener.Stop as part of the TheadAbortException handler and tried to catch an EndAcceptSocket call (since the socket is placed by Stop)

Is this really the best approach that starts and stops listening on socket connections?

+6
multithreading c # sockets
Apr 25 '11 at 12:21
source share
1 answer

The reason Thread.Abort does not interrupt the thread that is listening on the socket is because this thread is blocked inside the (moral equivalent) of the listen() kernel call. ThreadAbortException can only be raised by the CLR only when the CLR is executed, and during a call to listen thread actually gets stuck inside an unmanaged system call. After the listen call returns and execution resumes within the CLR, thread abortion can continue.

Secondly, the advice is not to use Thread.Abort() . If you decide to go this route, you can use a technique similar to the one shown in your second example. However, you'd better use a different method and just call Listener.Stop() if you want to disable the stream listener.

+7
Apr 25 2018-11-12T00:
source share



All Articles