Error when my Win32 Service takes longer than 30 seconds to shut down.

B

Bill Davidson

All:

I have a Win32 service that takes about 30 seconds to shutdown (give or take
a few seconds). I shut the service down via the 'Services' console on
Windows Server 2003. When the service shuts down in under 30 seconds,
everything is fine. However if the service takes over 30 seconds to shut
down, I get the following error in the System Event Log:

"Timeout (30000 milliseconds) waiting for a transaction response from the
XYZ service."

I get this error even though I frequently increment the 'dwCheckPoint' value
via a call to SetServiceStatus() during the shutdown sequence.

From the MSDN docs on the SetServiceStatus() API, bumping this value should
inform the SCM that my service needs a little more time to shutdown.

Any ideas?

Thanks,
Bill
 
J

Jeffrey Tan[MSFT]

Hi Bill,

Windows service has very restricted rule for the control code operation.
Basically, the HandlerEx function(this function is encapsulated in .Net
Windows Service to call your OnStop, OnShutdown handlers) must return
within 30 seconds, or the SCM will think that the service has stopped
responding.

SetServiceStatus with dwCheckPoint and dwWaitHint members is merely a hint
to the SCM how long it taks, it can not be used to delay the shutdown of
service or system. Jeffrey Richter's book <Programming Server-Side
Applications for Microsoft Windows 2000> "Control Codes and Status
Reporting" section in chapter2 talks about these rules in details.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Bill Davidson

Jeffrey:

Thanks for the good information. Based on your reply, I found the following
additional information for theService Control Handler function under
http://msdn2.microsoft.com/en-us/library/ms685149.aspx

"The control handler must return within 30 seconds, or the SCM returns an
error. If a service must do lengthy processing when the service is executing
the control handler, it should create a secondary thread to perform the
lengthy processing, and then return from the control handler. This prevents
the service from tying up the control dispatcher. For example, when handling
the stop request for a service that takes a long time, create another thread
to handle the stop process. The control handler should simply call
SetServiceStatus with the SERVICE_STOP_PENDING message and return.."

Once I did this, everything worked fine. I returned from the handler
quickly (just after calling SetServiceStatus with a status of
SERVICE_STOP_PENDING). On a separate thread, I proceeded with my lenghty
shutdown logic, bumping the dwCheckPoint value at periodic intervals to let
the SCM know the service wasn't hung. When my shutdown logic had completed
(about 60 seconds later), I again called SetServiceStatus with a status of
SERVICE_STOPPED, and the 'Service Console' then correctly showed the service
as stopped. Both the SCM and my service we're happy (i.e. no errors
reported from either)..

Thanks also for the Richter book reference. He is an outstanding author.

Bill

"Jeffrey Tan[MSFT]" said:
Hi Bill,

Windows service has very restricted rule for the control code operation.
Basically, the HandlerEx function(this function is encapsulated in .Net
Windows Service to call your OnStop, OnShutdown handlers) must return
within 30 seconds, or the SCM will think that the service has stopped
responding.

SetServiceStatus with dwCheckPoint and dwWaitHint members is merely a hint
to the SCM how long it taks, it can not be used to delay the shutdown of
service or system. Jeffrey Richter's book <Programming Server-Side
Applications for Microsoft Windows 2000> "Control Codes and Status
Reporting" section in chapter2 talks about these rules in details.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
J

Jeffrey Tan[MSFT]

Hi Bill,

Sorry for the late response. I am out of office these days.

Oh, thank you for the sharing. I originally believe that it is impossible
for SCM to wait more than 30 seconds; while the actual algorithm for SCM is
that it will not allow the main control handler thread to process more than
30 seconds. It seems that this all depends on the SCM internal algorithm.

Anyway, I do not think delay the shutdown of SCM is a good design, because
it will also delay the shutdown process of the entire system, which causes
not good UI experience to the end user.(The user may believe the shutdown
operation failed due to the length)

Maybe you should employ some type of early saving logic in your service. It
means that you may save part of your work very few time periods, so the
burden of shutdown will reduce. Anyway, you may live with the current
solution before doing a lot of improvement.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top