My NT Services crash at startup (1 out of 10 time)

  • Thread starter Thread starter José Joye
  • Start date Start date
J

José Joye

On all my Windows Services (written in C# and for some of them in MC++), I
got from time to time crashes at startup (1 out of 10 startup). It occurs
really at the time I click on the "Start service" button.

I have try/catch sections all around, and also have defined an
"UnhandledExceptionEventHandler" in the ctor of the Service Main().
I'm still not able to catch what is going on :-((

The error is:

I do suspect that this may occurs in one of my common assemblies. Probably
in some static member/class initializations.
What I did is to try to place suspect static initializations in a class
initializer and place them within a try/catch sections.

I tried to start the service as a normal program (according to description
shown in MSDN). However, it works ok this way.


Here are my questions:

- If I place a try/catch section in a class initializer, will the catch be
hit in case of problem?
- How could I point out to the location where the problem occured using the
error message shown
- Is it possible to debug the application?
If I press Cancel, DrWatson generates a log. I would like to run JIT
debugger instead


Any help greatly appreciated!!!
José
 
Some ideas:

You could put some EventLog.WriteEntry entries in your common assemblies just to try and pinpoint the problem.
Also, make sure that none of your common assemblies inadvertently does any UI.
Verify this by setting the service to 'Allow interaction with desktop' and running the service.

Also; there was a bug in v1.0 of the framework, not sure if it's in v1.1, where services couldn't start if
the eventlog is full. Try clearing the eventlog and see if it helps.
 
Thanks for the tips.

Sorry for information I missed:

- I currently write into the EventLog as fallback in my tracing routines.
- I already allowed interaction with the Desktop to see if I can get extra
info
- I have set the overwrite eventlog flag (so it will not get full).

Thanks anyway!
José

Robert Jeppesen said:
Some ideas:

You could put some EventLog.WriteEntry entries in your common assemblies
just to try and pinpoint the problem.
Also, make sure that none of your common assemblies inadvertently does any UI.
Verify this by setting the service to 'Allow interaction with desktop' and running the service.

Also; there was a bug in v1.0 of the framework, not sure if it's in v1.1,
where services couldn't start if
 
You can attach to the process from within Visual Studio. It's in the debug menu.
Since your service causes a crash in the constructor, start the constructor with
a Thread.Sleep(15000); This will give you time to attach to the process before it crashes.
Also, if the service crashes within 15 seconds, you'll know that it''s probably an initialization in
a variable declaration that is crashing, because such initializasions get compiled in the start of the constructor.
 
José Joye said:
On all my Windows Services (written in C# and for some of them in MC++), I
got from time to time crashes at startup (1 out of 10 startup). It occurs
really at the time I click on the "Start service" button.

I have try/catch sections all around, and also have defined an
"UnhandledExceptionEventHandler" in the ctor of the Service Main().
I'm still not able to catch what is going on :-((

The error is:


memory could not be "read".

You may have a bad memory module also. Does this happen on only one
computer or on a number of them?
 
Hello,

This happens on high speed (4 CPUs) computers. I really suspect
initialization problem.

José
 
José Joye said:
Hello,

This happens on high speed (4 CPUs) computers. I really suspect
initialization problem.

This code snippet may help you out. I use this to be able to switch
from running as a service to running the application in a debugger.
Obviously 'runasService' is a boolean type.
By running it in a debugger you may be able to figure out what is
going on.

// The main entry point for the process
static void Main(String[] args)
{
// Creates an instance of the methods that will handle the exception.
//CustomExceptionHandler eh = new CustomExceptionHandler();

// Adds the event handler to to the event.
//Application.ThreadException += new
ThreadExceptionEventHandler(eh.OnThreadException);

if (runAsService)
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
LabLoginService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
else
{
LabLoginService service = new LabLoginService();
service.OnStart(args);
}
}
 
Thanks,

I will try it

José
James Black said:
José Joye said:
Hello,

This happens on high speed (4 CPUs) computers. I really suspect
initialization problem.

This code snippet may help you out. I use this to be able to switch
from running as a service to running the application in a debugger.
Obviously 'runasService' is a boolean type.
By running it in a debugger you may be able to figure out what is
going on.

// The main entry point for the process
static void Main(String[] args)
{
// Creates an instance of the methods that will handle the exception.
//CustomExceptionHandler eh = new CustomExceptionHandler();

// Adds the event handler to to the event.
//Application.ThreadException += new
ThreadExceptionEventHandler(eh.OnThreadException);

if (runAsService)
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new
Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
LabLoginService() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
else
{
LabLoginService service = new LabLoginService();
service.OnStart(args);
}
}
 
Back
Top