Cloning Git repository throwing ArithmeticException

417 views Asked by At

We're utilizing Bonobo Git Server to host some internal git repos. When attempting to check out one of our repositories we're getting this error returned:

RPC Failed; result=22, HTTP code = 500

fatal: The remote end hung up unexpectedly

In the Windows Event Viewer it logs this message:

Exception information: 
    Exception type: ArithmeticException 
    Exception message: Overflow or underflow in the arithmetic operation.
Request information: 
    Request URL: http://localhost:50287/MyRepo.git/git-upload-pack 
    Request path: /MyRepo.git/git-upload-pack 

If I debug Bonobo locally no exception is thrown in C#; it comes from the outstream of the git process. The code utlizes Process to run git.exe like so:

using (var process = System.Diagnostics.Process.Start(info))
{
    inStream.CopyTo(process.StandardInput.BaseStream);
    process.StandardInput.Write('\0');
    process.StandardOutput.BaseStream.CopyTo(outStream);

    process.WaitForExit();
}

The command arguments passed to git are:

upload-pack --stateless-rpc D:\PathToRepos\MyRepo

If I run git.exe with the clone command from a command prompt, the project clones properly (with a warning of templates not found)

I'm thinking that it's a datatype issue between C# and what git is streaming to the Response.OutputStream.

1

There are 1 answers

0
Dave Zych On BEST ANSWER

The issue was due to the response buffering the output. It would attempt to buffer the entire stream in memory prior to sending it and with large repositories this caused the ArithmeticException. Since Response.Buffer defaults to true, it must explicitly be set to false prior to sending the data. The data also has to obviously be read and streamed in chunks.

Response.Buffer = false;

while ((read = process.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
{
    Response.OutputStream.Write(buffer, 0, read);
    Response.OutputStream.Flush();
}