Romil Kumar Jain
Romil Kumar Jain

Reputation: 20745

Window service automatically get stopped due to error?

I have an windows service that runs 24*7. This service use Renci.SshNet (third party dll) to connect to sftp server and downloading and uploading the files.

I got following information in event viewer. I check the log files and found that when this error come, the code was not running related to SFTP server.

I have written all the code in try catch block, so every error is logged into the log files.

Due to this error, windows service stopped.

I want to know the root cause of this issue and how I can modify the code so my service never get stopped again automatically.

Log Name:      Application
Source:        .NET Runtime
Date:          5/12/2012 10:49:12 AM
Event ID:      1026
Task Category: None
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      FedEx-EDI-01
Description:
Application: Ess.SupplyChainService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.Sockets.SocketException
Stack:
   at Renci.SshNet.Session.WaitHandle(System.Threading.WaitHandle)
   at Renci.SshNet.Channels.Channel.Dispose(Boolean)
   at Renci.SshNet.Channels.ChannelSession.Dispose(Boolean)
   at Renci.SshNet.Channels.Channel.Dispose()
   at Renci.SshNet.Sftp.SubsystemSession.Dispose(Boolean)
   at Renci.SshNet.Sftp.SftpSession.Dispose(Boolean)
   at Renci.SshNet.Sftp.SubsystemSession.Finalize()

Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
  <System>
    <Provider Name=".NET Runtime" />
    <EventID Qualifiers="0">1026</EventID>
    <Level>2</Level>
    <Task>0</Task>
    <Keywords>0x80000000000000</Keywords>
    <TimeCreated SystemTime="2012-05-12T14:49:12.000Z" />
    <EventRecordID>3723</EventRecordID>
    <Channel>Application</Channel>
    <Computer>FedEx-EDI-01</Computer>
    <Security />
  </System>
  <EventData>
    <Data>Application: Ess.SupplyChainService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.Sockets.SocketException
Stack:
   at Renci.SshNet.Session.WaitHandle(System.Threading.WaitHandle)
   at Renci.SshNet.Channels.Channel.Dispose(Boolean)
   at Renci.SshNet.Channels.ChannelSession.Dispose(Boolean)
   at Renci.SshNet.Channels.Channel.Dispose()
   at Renci.SshNet.Sftp.SubsystemSession.Dispose(Boolean)
   at Renci.SshNet.Sftp.SftpSession.Dispose(Boolean)
   at Renci.SshNet.Sftp.SubsystemSession.Finalize()
</Data>
  </EventData>
</Event>

Upvotes: 1

Views: 5608

Answers (4)

Dennis
Dennis

Reputation: 37760

According to the stack trace you provided, exception raised in finalization thread due to garbage collection (see "at Renci.SshNet.Sftp.SubsystemSession.Finalize" line in stack trace).

Because you can't catch this exception, it becomes unhanded and stops your process.
And that's why "the code was not running related to SFTP server".

Also, if you look at the stack trace more attentively, there's "at Renci.SshNet.Channels.Channel.Dispose()" line. This line means, that 3rd-party library developer trying to release managed resources during finalization. There are very little cases, when this is useful. But, in the same time, it is very dangerous and error-prone.

You should contact library developers.
As a temporary workaround you can setup service restart.

Upvotes: 1

Felice Pollano
Felice Pollano

Reputation: 33242

Windows services stops to work as any other app if there is unhandled exceptions, that is your case. Try to debug the code ( having the service running as a console application is my strategy, but you can use attach to process as well ) to check why the exception happens, and handle it properly: a service should recover in some reliable way, the restart on error option and friends should be used just as extreme solution, for instance if you don't own the code.

Upvotes: 0

Richard
Richard

Reputation: 108975

An unhanded exception will bring your process down.

One answer is to handle the exception, but:

  • Just handling an exception does not ensure that the process is in a good state.
  • There is a small set of exceptions (eg. StackOverflowException) that cannot be caught: there is no way for a process to reliably return to a known state.

Alternatively:

  1. Configure the service (via Services.msc) to restart on error,
  2. Configure a tool like adplus (from Debugging Tools for Windows) to take a process dump on failure.
  3. Uses those process dumps to fix the underlying errors.

Upvotes: 2

Oded
Oded

Reputation: 498904

You got a socket exception there - the stack trace you posted doesn't contain enough information as to why this happened.

Since the exception was not handled, the process terminated and of course the service stopped as result.

You need to find out why the exception occurred - add more logging and try to capture the incoming socket data.

Upvotes: 3

Related Questions