Is it possible for a SERVICE_WIN32_OWN_PROCESS service to cleanly
stop itself while being started i.e. from inside its own SvcMain function.
“Cleanly” meaning that:
(a) the service never enters the SERVICE_RUNNING state
before stopping for good;
(b) doesn’t trigger an error, or entry with level=error in the event logs;
(c) the SCM actually unloads the .exe as soon as the service sets itself
to a SERVICE_STOPPED status, as opposed to waiting for 30 sec,
which seems to be the default associated with various SC requests.
I tried to SetServiceStatus to SERVICE_STOPPED in SvcMain (right after
RegisterServiceCtrlHandler) which does indeed “stop” it before ever
starting, and doesn’t log an error (except a rather benign “could not be
started … did not report an error … helpmsg 3534” at the cmd prompt).
But the .exe is left hanging in task manager for another 30 seconds with
no good reason. I also tried several permutations of setting the status
to SERVICE_START_PENDING or SERVICE_STOP_PENDING in SvcMain
(with or without a dwWaitHint of 1 msec) then setting SERVICE_STOPPED
in a worker thread spawned by SvcMain, yet those 30 sec still persist.
Is it possible for a SERVICE_WIN32_OWN_PROCESS service to cleanly
stop itself while being started i.e. from inside its own SvcMain function.
“Cleanly” meaning that:
(a) the service never enters the SERVICE_RUNNING state
before stopping for good;
(b) doesn’t trigger an error, or entry with level=error in the event logs;
(c) the SCM actually unloads the .exe as soon as the service sets itself
to a SERVICE_STOPPED status, as opposed to waiting for 30 sec,
which seems to be the default associated with various SC requests.
I tried to SetServiceStatus to SERVICE_STOPPED in SvcMain (right after
RegisterServiceCtrlHandler)…
That is the right way to do it. The garbage collection clean-up cycle
within SCM is not under your control.
Is there a reason this causes a problem, or is it just annoyance?
–
Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.
Liviu wrote:
> …I tried to SetServiceStatus to SERVICE_STOPPED in SvcMain
> (right after RegisterServiceCtrlHandler)…
That is the right way to do it. The garbage collection clean-up cycle
within SCM is not under your control.
Is there a reason this causes a problem, or is it just annoyance?
No real problem, mostly annoyance plus a bit of hesitation. Windows
services is not my daily cup of tea, and I wasn’t sure whether that lag
might perhaps hint at some deeper flaw. Thank you for clearing it up.
It could be Windows is holding the service process alive in anticipation that the service is being restarted. Not sure how much sense it makes anymore, though.
wrote: > It could be Windows is holding the service process alive in anticipation > that the service is being restarted.
That would be a sensible presumption, but I don’t see it happening. In a mock test starting the service 5 times in quick succession, and set up as to “stop while starting” every time, I am getting 5 different PIDs and I can see the 5 processes still lagging for 30 sec in task manager afterwards.
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:bounce-597177- xxxxx@lists.osr.com] On Behalf Of Liviu
Sent: 04 December 2015 08:14
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] service auto-stopping during start
wrote: > > It could be Windows is holding the service process alive in > > anticipation that the service is being restarted. > > That would be a sensible presumption, but I don’t see it happening. > In a mock test starting the service 5 times in quick succession, and > set up as to “stop while starting” every time, I am getting 5 different > PIDs and I can see the 5 processes still lagging for 30 sec in task > manager afterwards. > > Liviu This email message has been delivered safely and archived online by Mimecast.
Thank you for the hint. It was definitely the ServicesPipeTimeout 30 sec
default. If I changed that in the registry, the lag changed accordingly.
That said, culprit was the custom entry point in the service code,
which was doing a “return” instead of “ExitProcess”. SCM still had
an active thread at that point, which caused the process to hang on
until the timeout expired. Once I put the due “ExitProcess” in place, the
.exe unloaded as soon as stopped. Obvious, I know, and silly as well.
Thanks all for the pointers, and sorry for the noise.
-----Original Message-----
From: xxxxx@lists.osr.com [mailto:bounce-597177- xxxxx@lists.osr.com] On Behalf Of Liviu
Sent: 04 December 2015 08:14
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] service auto-stopping during start
wrote: > > It could be Windows is holding the service process alive in > > anticipation that the service is being restarted. > > That would be a sensible presumption, but I don’t see it happening. > In a mock test starting the service 5 times in quick succession, and > set up as to “stop while starting” every time, I am getting 5 different > PIDs and I can see the 5 processes still lagging for 30 sec in task > manager afterwards. > > Liviu