Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle a thread that has a hung call?

I have a thread that goes out and attempts to make a connection. In the thread, I make a call to a third party library. Sometimes, this call hangs, and never returns. On the UI thread, I want to be able to cancel the connection attempt by aborting the thread, which should abort the hung call to the third party library.

I've called Thread.Abort, but have now read that Thread.Abort only works when control returns to managed code. I have observed that this is true, because the thread never aborts, and I've been sitting on Thread.Join for ten minutes now. What should I do with this hung thread? Should I just null the reference and move on? I'd like to be as clean as possible--

like image 950
MedicineMan Avatar asked Jun 10 '09 22:06

MedicineMan


People also ask

How does Java handle hung threads?

The Java ThreadPool does not have a mechanism for detecting hanging threads. Using a strategy like fixed threadpool ( Executors. newFixedThreadPool() ) will not work because if some tasks hang over time, all of the threads will eventually be in a hung state.

What happens when thread goes out of scope?

The thread continues to execute until the thread procedure is complete. So even if the Thread object is unreferenced, the thread will still run. Save this answer.

What is a calling thread?

A calling thread is the thread that calls a method or the thread inside which a method is called. If thread1 calls method methodA (if methodA gets called from within thread1 ) then the calling thread of methodA is thread1 . The listener argument specifies a callback method that will be called later in time.


2 Answers

Random thought: I wonder if you could write a second assembly as a small console exe that does this communication... launch it with Process.Start and capture results either via the file system or by intercepting stdout. Then if it hangs you can kill the process.

A bit harsh, maybe - and obviously it has overheads of spawning a process - but it should at least be possible to kill it.

like image 193
Marc Gravell Avatar answered Sep 22 '22 14:09

Marc Gravell


This function in your third-party library doesn't have a timeout or cancel function? If so, that's pretty poor design. There's not going to be any pretty solution here, methinks...

Unfortunately, there's no way you're going to get around it, short of using the Win32 API to kill the thread manually, which is certainly not going to be clean. However, if this third-party library is not giving you any other options, it may be the thing to do. The TerminateThread function is what you'll want to use, but observe the warning! To get the thread ID to pass to this function, you have to use another Win32 API call (the Thread class doesn't expose it directly). The approach here will be to set the value of a volatile class variable to the result of GetCurrentThreadId at the start of the managed thread method, and then use this thread ID later to terminate the thread.

like image 40
Noldorin Avatar answered Sep 21 '22 14:09

Noldorin