Search code examples
windowsvb.netservicetimervolume-shadow-service

VSS backups cause custom windows service to fail


I've built custom windows services in vb.net that run on a timer and look for files in a remote directory to process. On a few servers, they fail without any error at the same exact time, around 10:04PM when the VSS service fails. The service errors look like:

'The process cannot access the file filename.txt because it is being used by another process.'

Sometimes there are no errors but the service hangs. I am assuming this is the VSS service. Regardless, there is a huge spike in CPU up to 100% and back down. I have logs (ie the file above that cannot be written to) that relay all activity to me, and each process is wrapped in a try..catch which will also report exceptions to logs for me. Nothing comes of this. The processes fail in random parts of code, usually in the poll timer and sometimes in the middle of processing without warning. The service hangs but says it's still running in services.msc.

Has anyone experienced this issue or know of a fix? I was thinking to make the services multi-threaded so one can check on another in case one thread hangs but am not sure if this would fix the problem. We have tried giving more resources to the servers and this seems to have helped a bit but did not completely fix the problem. Some days it overloads, others it doesnt. Using windows server 2008 x64.

Thanks in advance for you help!

    Public Shared aTimer As New Timers.Timer

    Public Shared Sub SetTimer()
        aTimer.Interval = 60000
        ' Hook up the Elapsed event for the timer.  
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        aTimer.Enabled = True

    End Sub

    ' The event handler for the Timer.Elapsed event.  
    Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        Dim gen = New Service1
        aTimer.Enabled = False
        gen.ProcessEvents() 'This is the main function of the service
        aTimer.Enabled = True
    End Sub

Solution

  • Update:

    Had never thought of a simple solution like this, but in the Task Manager, I right clicked on the service and set the priority to "Realtime" in hopes that the high CPU load during backups would not affect the service. So far so good.

    This can be done programatically so each time the service restarts it is in higher priority. This is in my OnStart():

    VB.NET:

    System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.RealTime