0

I have a process, which runs in a console app. It runs forever.

After a couple of days the app crashes with a StackOverflowException.

The essence of the app is where I spin up a Process with FFMpeg.exe and creates a sceenshot of a video stream. It works very good but only for a few days at the time.

I am pretty sure it has to do with the disposal of the FFMpeg or some internal Process stuff.

Here is the code

using ( Process ffmpegProcess = new Process() ) {

    //arguments for running ffmpeg
    ffmpegProcess.StartInfo.UseShellExecute = false;
    ffmpegProcess.StartInfo.CreateNoWindow = true;
    ffmpegProcess.StartInfo.RedirectStandardOutput = true;

    //specific for our screenshots
    ffmpegProcess.StartInfo.FileName = string.Concat( Environment.CurrentDirectory, Path.DirectorySeparatorChar, ffmpegProgramName );

    try {
        //todo: log this stopwatch somewhere perhaps
        processWatch.Start();

        //set arguments every time we want to create a new screen shot
        ffmpegProcess.StartInfo.Arguments = string.Format( @"-y -i {0}{1} -threads 0 -ss 00:00:01.000 -f image2 -s 620x349 -vframes 1 ../../web/{2}.jpg", server, streamPath, slug );
        ffmpegProcess.Start();
        ffmpegProcess.WaitForExit( 500 );

        Console.WriteLine( slug );
        Console.WriteLine( processWatch.Elapsed );

        processWatch.Reset();
        runCount++;
        cacheIndexer++;

        //lets see how many spins we've had!
        Console.WriteLine( string.Format( "SERVER CACHE INDEX : {0}", cacheIndexer ) );
        Console.WriteLine( string.Format( "RUN : {0}", runCount ) );
        Console.WriteLine( Environment.NewLine );

    } catch ( Exception ex ) {
        //Console.WriteLine( "Ex " + ex );
    }
}

The loop looks like this.

    public void RecurseTask() {
        /*
        You can try one of these, but you will se CPU usage go crazy and perhaps concurrency errors due IO blocking

        Parallel.ForEach( _videoStreamSlugs, ( e ) => _videoStreamScreenShots.GrabScreenShot( e ) );

        foreach ( var slug in _videoStreamSlugs ) {
            Task.Run( () => _videoStreamScreenShots.GrabScreenShot( slug ) );
        }
        */

        //we want to grab screen shots for every slug in out slug list!
        foreach ( var slug in _videoStreamSlugs ) {
            _videoStreamScreenShots.GrabScreenShot( slug );
        }

        //sleep for a little while
        Thread.Sleep( _recurseInterval );

        //A heavy clean up!
        //We do this, trying to avoid a stackoverflow excecption in the recursive method
        //Please inspect this if problems arise
        GC.Collect();

        //lets grab over again
        RecurseTask();
    }

I added a GC.Collect out of curiosity to see if it made a difference.

I am not doing a Windows Service.

user3633222
  • 177
  • 1
  • 7
  • 2
    Can you show the full loop? – Matteo Umili Jun 04 '15 at 07:36
  • Perhaps think about using a Windows Service and instead of an infinite loop (sounds like that is what you are using?), try a use a timer or filewatcher in the windows service. Maybe this can also help: http://stackoverflow.com/questions/206820/how-do-i-prevent-and-or-handle-a-stackoverflowexception – Praval 'Shaun' Tirubeni Jun 04 '15 at 08:20

1 Answers1

2

Inside RecurseTask you always call RecurseTask, obviously, when long running it will throw a StackOverflowException, you can try to change to

public void RecurseTask() {
    while(true)
    {
        /*
        You can try one of these, but you will se CPU usage go crazy and perhaps concurrency errors due IO blocking

        Parallel.ForEach( _videoStreamSlugs, ( e ) => _videoStreamScreenShots.GrabScreenShot( e ) );

        foreach ( var slug in _videoStreamSlugs ) {
            Task.Run( () => _videoStreamScreenShots.GrabScreenShot( slug ) );
        }
        */

        //we want to grab screen shots for every slug in out slug list!
        foreach ( var slug in _videoStreamSlugs ) {
            _videoStreamScreenShots.GrabScreenShot( slug );
        }

        //sleep for a little while
        Thread.Sleep( _recurseInterval );

        //A heavy clean up!
        //We do this, trying to avoid a stackoverflow excecption in the recursive method
        //Please inspect this if problems arise
        //GC.Collect(); Not needed

        //lets grab over again
    }
}
Matteo Umili
  • 3,412
  • 1
  • 19
  • 31
  • I am not sure I understand why that is, though ? To me it's just recursion, and calling the same method over again seems like the same thing you are doing with a while loop `? – user3633222 Jun 05 '15 at 06:53
  • Every time you call a function, the return address (arguments and local variables) is pushed on the call stack. The call stack has a fixed size, so in infinite recursion, you will run out of stack space. – Matteo Umili Jun 05 '15 at 07:13
  • Sometimes you get blinded by something really simple stupid. Thanks so much! – user3633222 Jun 05 '15 at 08:03