3

Running a Git diff gets stuck, till killed when running as a System.Diagnostics.Process.

Code:

class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo pInfo = new ProcessStartInfo();
            pInfo.FileName = "git.exe";
            pInfo.Arguments = "diff --name-only --exit-code V2.4-Beta-01 HEAD";
            pInfo.WorkingDirectory = @"C:\Git";
            pInfo.UseShellExecute = false;
            pInfo.CreateNoWindow = true;
            pInfo.RedirectStandardError = true;
            pInfo.RedirectStandardOutput = true;

            Process p = new Process();
            p.StartInfo = pInfo;

            p.Start();

            p.WaitForExit(10000);

            if (!p.HasExited)
            {
                p.Kill();
                Console.WriteLine("Killed!!!");
            }

            Console.WriteLine(p.StandardOutput.ReadToEnd());
            Console.WriteLine(p.StandardError.ReadToEnd());
            Console.ReadLine();
        }
    }

How to avoid this and make the program exists normally without expiring its timeout?

Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28
Luis
  • 2,833
  • 3
  • 27
  • 41
  • Just tested your code on my machine with a git project and it worked fine. Do you have more info? What happens if you run the command in Git Bash. But just as a sidenote: When installing git I opted for being able to use git in command line as well. – Uwe Hafner Apr 30 '15 at 16:35
  • I'm running it in Windows (so .NET, no Mono), and I added the "--exit-code" to avoid getting a ":" at the end to type "q" (quit) when runnning the command in the command line. But when run from .NET it still gets stucked. I don't know if that helps. Is that any Git configuration issue? I've surfed the web and the diff command help with no success – Luis Apr 30 '15 at 16:37
  • I don't have to enter q to quit even without --exit-code. The command exits in Command line automatically. What git did you install? from git-scm.com version? I have version 1.9.5 – Uwe Hafner Apr 30 '15 at 19:06
  • The same version as you. Do you hace actually differences when running the command? Otherwise the process finishes fine. – Luis Apr 30 '15 at 19:09
  • if I use the command I get a list of files and the command finishes. – Uwe Hafner Apr 30 '15 at 19:10
  • could it be that your command takes longer than 10 seconds to complete? – Uwe Hafner Apr 30 '15 at 19:14
  • Nope, i used the same method without timeout and it happens the same. I'll review my git settings, just in case – Luis Apr 30 '15 at 19:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76671/discussion-between-uwe-and-luis). – Uwe Hafner Apr 30 '15 at 20:30

1 Answers1

2

The problem is that someone has to consume the stdout buffer or it will get filled and the process gets stucked (see explanation here). The diff I was trying retrieved 983 lines, which was causing a buffer overflow.

The following is a solution to my problem:

class Program
    {
        static void Main(string[] args)
        {
            ProcessStartInfo pInfo = new ProcessStartInfo();
            pInfo.FileName = "git.exe";
            pInfo.Arguments = "diff --name-only --exit-code V2.4-Beta-01 HEAD";
            pInfo.WorkingDirectory = @"C:\Git";
            pInfo.UseShellExecute = false;
            pInfo.CreateNoWindow = true;
            pInfo.RedirectStandardError = true;
            pInfo.RedirectStandardOutput = true;

            string output = string.Empty;

            Process p = new Process();
            p.StartInfo = pInfo;

            p.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
            {
                if (!String.IsNullOrEmpty(e.Data))
                {
                    output += e.Data + Environment.NewLine;
                }
            });

            p.Start();

            p.BeginOutputReadLine();

            p.WaitForExit();
            p.Close();

            Console.WriteLine(output);
            Console.ReadLine();
        }
    }
Community
  • 1
  • 1
Luis
  • 2,833
  • 3
  • 27
  • 41