Re: [topshelf-discuss] Unhandled exception does not cause restart

640 views
Skip to first unread message

Chris Patterson

unread,
Mar 20, 2013, 5:33:42 PM3/20/13
to topshelf...@googlegroups.com
There is a way post-XP to configure a service to Restart on a non-zero exit code (if the service recovery options are specified). I've added that to develop for the next release of Topshelf, so that when the service shuts down with a non-zero exit code it will be restarted by Windows using the defined restart policy.



On Tue, Mar 19, 2013 at 11:25 AM, dunkymole <dunca...@gmail.com> wrote:
Hi,
 
My service is deployed as a windows service. It has been configured to restart on error. I am on 3.1.0 from nuget.
 
Theres a bug lurking in some library code I use and cannot getaway from which, under certain circumstances, raises an exception from the threadpool. When this happens, the host detects it and shuts down my serivice. However, the SCM does not restart the service, I suspect because it thinks it was a graceful shutdown. Heres my host program:
 
static int Main(string[] args)
{
  return (int)HostFactory.Run(x =>
  {   
  x.UseNLog();
  x.Service<SyncHostContainer>(s =>
  {
    s.ConstructUsing(name => new SyncHostContainer());
    s.WhenStarted(tc => tc.Start());
    s.WhenStopped(tc => tc.Stop());
  });
  x.RunAsLocalSystem();
  x.SetDescription("blah");
  x.SetDisplayName("blah");
  x.SetServiceName("blah");
});
}
 
I have had a little dig in the service host and I see it hooking up the unhandled exception event and shutting sown if it occurs. I can't see it assigning an error code anywhere though. I am also curious about the thread sleep for an hour, so any info about that would be good.
 
Cheers
 

--
You received this message because you are subscribed to the Google Groups "topshelf-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topshelf-discu...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

dunkymole

unread,
Mar 21, 2013, 5:23:49 AM3/21/13
to topshelf...@googlegroups.com
Right. Exactly, the problem is the zero exit code despite the error. I have added my own unhandled exception handler with Environment.Exit(-1) which does the trick for now. Still curious about the thread sleep, can you shed some light on that?
Cheers and thanks for the great library,

Chris Patterson

unread,
Mar 22, 2013, 4:23:14 PM3/22/13
to topshelf...@googlegroups.com
Without blocking the unhandled exception thread, the service will fall apart in a miserable fashion. By signaling the service to exit, the intent it allow for a clean exit with a non-zero exit code (which as you've found is not being set).

I'm going to verify and add the non-zero exit for fatal shutdown situations.

Jesse Beard

unread,
Apr 5, 2013, 4:00:26 PM4/5/13
to topshelf...@googlegroups.com
Thanks Chris.  This may help towards my Recovery Options not being honored for restarting my services.  I add the recovery options manually using sc.exe, but they are not being honored as before, so I think this may fix that.

Jesse Beard

unread,
Apr 11, 2013, 2:43:27 PM4/11/13
to topshelf...@googlegroups.com
Chris, when do you plan on releasing this update to nuget?


On Friday, March 22, 2013 4:23:14 PM UTC-4, Chris Patterson wrote:

Chris Patterson

unread,
Apr 11, 2013, 4:52:14 PM4/11/13
to topshelf...@googlegroups.com
This should already be on 3.1.1, which was released a week or two ago.

Jesse Beard

unread,
Apr 12, 2013, 8:13:48 AM4/12/13
to topshelf...@googlegroups.com
Durrr, yep I got it.  Thanks.

dunkymole

unread,
Apr 17, 2013, 6:39:50 AM4/17/13
to topshelf...@googlegroups.com
Hey Chris,
A belated thanks for this.
Cheers

Chris Patterson

unread,
Apr 18, 2013, 1:31:46 PM4/18/13
to topshelf...@googlegroups.com
Glad to help :)
Reply all
Reply to author
Forward
0 new messages