Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes StartServiceCtrlDispatcher() to fail with 1063 (ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)?

I'm seeing weird error with my Windows service program. My service program calls StartServiceCtrlDispatcher() at the very beginning of its main(), but it sometime fails with 1063 (ERROR_FAILED_SERVICE_CONTROLLER_CONNECT).

I know that this error occurs if the user starts program manually (as a console program). But, it's not the case. I added a code to check the parent process of the service program when this error occurs, and it tells services.exe is the parent process (I think it's safe to assume that my program was properly started by SCM).

Unfortunately, this error does not reproduce on my dev machine and cannot debug it by myself, but the error logs captured on user systems tells:

  • This problem seems to happen on only few % of the all users of this program.
  • Even if the problem happens, it doesn't seem to repeat. Next time the service usually starts successfully.
  • When this problem happens, StartServiceCtrlDispatcher() stalls for about a second before it returns with fail.

Has anyone seen similar error? If so, what was the cause of the error?

like image 843
Susumu Arai Avatar asked May 26 '15 16:05

Susumu Arai


1 Answers

As you can already see from the absence of answers and anything on google, the problem isn't common. I believe the problem is in your service, AND it is in code executed from process's start to StartServiceCtrlDispatcher(), AND most likely it takes some form of corrupting system resources, likely heap or HANDLE's.

You can be sorry to hear this, but I'm not going to have a magic answer to your problems. Instead, I can suggest some troubleshooting.

Microsoft Application Verifier is invaluable in finding corruption. I suggest that you:

  1. Install it to your dev machine.
  2. Add your service's exe to it.
  3. Only select Basics\Heaps for the first time.
  4. Press Save. It doesn't matter if you keep application verifier open.
  5. Run your service a few times.
  6. If it crashes, debug it and the crash will point to your problem.
  7. If it doesn't crash, add Basics\Handles. Unlike Basics\Heaps, this can sometimes fire "false positives" - mistakes in code that doesn't hurt much. Anyway, since you're on the hunt, you'd better fix everything you can find. I'm mostly worried about double-freeing a HANDLE or something like that. Freeing a Service manager HANDLE by mistake can surely lead to your problem.
  8. If it still doesn't crash, you can try other options in Basics\*, but I don't think that will help.
  9. At this point, you might want to inspect the code between program's main() and StartServiceCtrlDispatcher(), and any global constructors you can have. Look for potential buffer overflows and errors with HANDLE's.
  10. The next step could be installing Application Verifier to the client's machine. There's not much harm in it, I sometimes do it when I can't find the error myself.
like image 97
Codeguard Avatar answered Nov 09 '22 03:11

Codeguard