Marcin
Marcin

Reputation: 12590

Window service stops, but process continues running

I am developing a Windows Service application, and noticed an unusual issue. First, I run the service from Windows Services window and wait for the status to turn to "Started". A few minutes later I click stop, and the service status turns to blank after 20 seconds or so, indicating the service has stopped. Yet the process behind this services continues running and can be seen in Task Manager for 10-30 seconds after the status has updated. I'm running on Windows 7sp1. I also checked Event Viewer, and it does not show any errors from my service. (The reason I mention this is because I initially suspected a timeout error of the form "A timeout was reached while waiting for a transaction response from the x service.")

Any ideas what could be causing the discrepancy between the process state and the service state?

Upvotes: 4

Views: 4794

Answers (1)

meklarian
meklarian

Reputation: 6625

This happens when one has other threads that continue to run that have no relation (or have become disassociated from) the threads related to your service.

Note that a process may be multi-tenant and host multiple services, so a stop command doesn't always imply that the owner process must exit, just that the service should stop running in that process.

To debug further, when you encounter this limbo state, you should attach a debugger and stop all threads and inspect the call stacks on each thread. For a single service process, you are likely to find that a thread has hung while waiting for IO or other operations to complete, or that there are idle threads such as message loop threads that are awaiting signals that will never be raised.

Upvotes: 6

Related Questions