C# Windows service hangs

A

Ashish Khandelwal

I have a Windows Service in C# talking to a serial port and using
Remoting.
It also uses several COM objects. On server the service will
occassionally hang somewhere - the service still shows on a Task
Manager
list. In Services it says it is still running. However there is no way
to
stop it other than by rebooting the whole computer. No exception
(including
non-CLS) is ever generated. I added a separate System.Timers.Timer
which
detects that the main thread stopped working. I was trying to stop it
by
using System.ServiceProcess.ServiceController.Stop() which didn't
work. I
tried to do process.Kill() which also doesn't work (and no exception).
Is
there a bug in framework somewhere. I don't even hope to find out why
it
hangs but if I could just kill it that would solve the problem since I
have
another Service which would bring it up. I'll be greatful for any
ideas.
 
M

Marc Gravell

Is there a bug in framework somewhere.
If this was my best code ever, my assumption would be that I'm doing
something wrong.
I don't even hope to find out why it hangs but if I could just kill it that would solve the problem since I
have another Service which would bring it up.
Ouch. I'd be wanting to know why... even if it was as simple as adding
a trace so I know what the thread(s) was (were) last doing .

It isn't exactly entry-level stuff, but if that fails, perhaps this is
a job for windbg / sos [assist]?

Marc
 
M

Marc Gravell

If this was my best code ever, my assumption would be that I'm doing
something wrong.

I read that again and realised it could be taken in two ways; I didn't
mean it in the derogatory way: more *even* if I was absolutely
confident that my code was right, I'd still suspect that I had gotten
something wrong somewhere that was messing this up.

Yes, the framework and Win32 do (both individually and in combination)
have bugs and features, but I'd try to rule out PEBKAC first.

Marc
 
A

Ashish Khandelwal

something wrong.

I read that again and realised it could be taken in two ways; I didn't
mean it in the derogatory way: more *even* if I was absolutely
confident that my code was right, I'd still suspect that I had gotten
something wrong somewhere that was messing this up.

Yes, the framework and Win32 do (both individually and in combination)
have bugs and features, but I'd try to rule out PEBKAC first.

Marc

Thanks for Reply, the 1st thing, the same code is running over the
last 3/4 years, there were no such type of issue, another, to give you
more deep information, i am using Multithread, and just before the
issue, the code was releasing the active threads, and creating some
new threads and doing some calculation internally - mean not
communicating with external resource like DB. Let me know please if
you need any more information.

Ashish
 
A

Ashish Khandelwal

One more thing, what would be the reason that i could not stop my
service, and there was only one way left which was reboot the system?
any idea plesae...
 
M

Marc Gravell

I'm assuming that you have tried killing the process from task manager
(you mention process.Kill(...); I interpret that as meaning "through
code")?

If you have a service that resists "kill", you're into some gnarly
hardcore debugging... sorry...

Marc
 
C

coolCoder

One more thing, what would be the reason that i could not stop my
service, and there was only one way left which was reboot the system?
any idea plesae...

You said that you are using multiple threads, so have you synchronized
objects between threads ? Try to use Monitor class in C#
Also, when dealing with windows service, use try catch block at all
necessary places, with strong logging facility. By logging, I mean to
say that you should know where the exception occurred and complete
stack trace of the Exception.
With this log, you can find the root cause for your problem and then
fix it.
I think you have changed code in existing windows service ? Am I
right ?

I would like to help you furthure, but for that I need the complete
log when the service "hangs". If you can provide that, it would be
helpful.
 
W

Willy Denoyette [MVP]

Ashish Khandelwal said:
I have a Windows Service in C# talking to a serial port and using
Remoting.
It also uses several COM objects. On server the service will
occassionally hang somewhere - the service still shows on a Task
Manager
list. In Services it says it is still running. However there is no way
to
stop it other than by rebooting the whole computer. No exception
(including
non-CLS) is ever generated. I added a separate System.Timers.Timer
which
detects that the main thread stopped working. I was trying to stop it
by
using System.ServiceProcess.ServiceController.Stop() which didn't
work. I
tried to do process.Kill() which also doesn't work (and no exception).
Is
there a bug in framework somewhere. I don't even hope to find out why
it
hangs but if I could just kill it that would solve the problem since I
have
another Service which would bring it up. I'll be greatful for any
ideas.


Attach a debugger to the hung service and inspect the stacks. My guess is
that you have deadlocked.
The reason (one of) you can't stop a service is that the service doesn't
react to a Stop request from the SCM, the SCM puts the service in a
"pending" stopped state and waits for the "Stopped" state indication from
the service, which never arrives. In this state the SCM can't do anything
further with the service, killing the service process doesn't help either,
the SCM has lost control over the service, all you can do is reboot :-(

Willy.
 
C

christery

if a racecondition occurs its not certain that your program knows
about it, but I did manage to stop the process by stopping the service
(it didnt stop) and then killing the process by hand in task manager,
this was in my case caused by not joining threads if I remeber
correctly. have not tried .kill, but .abort from code (just to see
what happens), and yes if lucky try/catch might get it (the error), in
my case when it (the servvice) didnt start I just got a silly mess in
eventlig that id didnt start in 3000ms or something... found a
debugging "help" free from aimes (I think hes called that) that helpt
me out.

Havent tred debugging any other way but the previus posters seems to
have som expreience, just get a idea of where it stopped cold could be
a great help.

//CY
 

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