Application hangs while waiting thread finalize

Application hangs while waiting thread finalize

Post by YUBpbnZhbG » Wed, 10 Nov 2004 03:56:05


I am currently working at a client-server application and eventually my
communication thread hangs while waiting a thread to finalize. It hangs at a
INFINITE WaitForSingleObject at the threads handle and about 20-30 seconds
latter the service crashes (according to system event log).

I tried a lot of possibilities but still dont have a clue of what is
happening, since I can not reproduce this error, the system can run for weeks
without having any problem.

My implementation uses the following strategy: When I notice that the thread
is no longer in use I set an event, at the communication thread, that
indicates the thread must exit and it begins to wait its ending:
WaitForSingleObject(thread_handle, INFINITE). Receiving the event
notification, the thread exits its loop, clean up and exits normally, as far
as I can see.

I dont have anything acessing this thread, dont need to treat any windows
message... My code is protected using a terminate_handler,
unexpected_handler, and exception handling and the process just vanish.

If anyone have a clue, a path the I should follow, anything that can help
will be appreciated.


Application hangs while waiting thread finalize

Post by JD » Wed, 10 Nov 2004 06:11:36



Wild guess -- In my experience when processes vanish and no exceptions
handlers are called, then its a stack problem of some sort.



Application hangs while waiting thread finalize

Post by Oleg Staro » Wed, 10 Nov 2004 18:20:06

In addition to the previous response:

What message is in the event log?

Do you have access to the system where it happens, or does it happen
only on a customer system far from your site?

Remove these handlers to make the problem more reproducible,
otherwise they (exception handlers) can hide the problem if they catch too much.
Use only C++ catch statements if needed, and do not use catch(...).

Better rely on out-of-process tools like Dr. Watson to get the log and crash dump.

Also make sure that you do not call SetErrorMode(SEM_NOGPFAULTERRORBOX)
function, since it disables JIT debugging.

Try the following:

* Download Debugging Tools for Windows, install it and in its documentation
read "Crash Dumps" chapter - it will give lots of useful information.

* On the system where the problem happens, register Dr. Watson as the JIT de ***
and configure it to produce log file and crash dump.

* Monitor the application with PerfMon to see if there are memory/resource leaks
(many such cases can be explained by leaks). Use counters under Process and Memory

* If the problem happens on your system, try to reproduce it under de *** .
When doing it, enable all available operating system checks (PageHeap,
AppVerifier (only since WinXP; use PageHeap, lock and handle verification).
Also set breakpoint on NtTerminateProcess in NTDLL.DLL, so that you will
always get control when the application is terminating, and will be able to check
the call stack and see exactly why it is terminating.

How to enable PageHeap:
If you have Debugging Tools for Windows, use "gflags -p /enable YourApp.exe /full"
to enable Full PageHeap.
Here you can find more information about it: ;en-us;286470
On Windows XP/2003 with AppVerifier installed, simply check PageHeap,
Locks and Handles for the application.

Consider also using tracing, it can tell you what exactly is going inside the application.


Application hangs while waiting thread finalize

Post by YUBpbnZhbG » Thu, 11 Nov 2004 00:57:08

"The NSC_Service service terminated unexpectedly. It has done this 1
time(s). The following corrective action will be taken in 0 milliseconds: no

Yes. I cant reproduce all the time, but I have access to two systems here
where it had already occurred.

Thanks a lot for the help, Ill try the steps you suggested bellow and than
Ill post a reply here to share the results.


Application hangs while waiting thread finalize

Post by YUBpbnZhbG » Thu, 11 Nov 2004 01:34:06

I know that I can have a stack problem as a stack overflow or when I have a
buffer overrun that trashes my methods return address. Is there any other

Im trying to verify this problem using a tracing utility but it didnt
occured yet. Ill keep trying...

Thanks for your reply.