Git unexpected disconnect while reading sideband packet – Code Example

Suppose you are downloading a 50GB movie and you have 1MBps or 8mbps connection. It will take around 50 x 1024 seconds. That is, around 15 hours. In these 15 hours your network might break and your download will stop. You get the error unexpected disconnect while reading sideband packet in git, due to number of reasons and bad network is one of them.

In this article we are going to learn about different solutions to this problem.

Why git throws this error?

If the repository is large there could be a number of possible reasons for something to go wrong. Due to this the operations could fail in the middle. For example, you are pushing changes but due to bad network it stopped. Or, allocated memory for git fall short. These events could lead to the error.

The error looks like this –

Enumerating objects: 27, done.
Counting objects: 100% (27/27), done.
Delta compression using up to 16 threads
Compressing objects: 100% (24/24), done.
Writing objects: 100% (25/25), 187.79 KiB | 9.39 MiB/s, done.
Total 25 (delta 1), reused 0 (delta 0), pack-reused 0
send-pack: unexpected disconnect while reading sideband packet
fatal: the remote end hung up unexpectedly

Solution 1: Removing Compression

Trying removing compression by setting it to 0 and retrieve the partial clone data. This will help in breaking the big chunk. Follow these steps –

1. Set compression to 0

git config --global core.compression 0

2. Fetch only partial clone data

git clone --depth 1 <your_repo_uri>

3. Now move to a new directory and fetch the remaining clone data

git fetch --unshallow 

4. git pull

git pull --all

Solution 2: Setting Memory Limits in .gitconfig

In .gitconfig file we can set the maximum memory that git can use for various operations. If the repository is large then it will require a subsequent amount of memory. You need to make sure that it doesn’t overflow.

To do this operation, you need to open your .gitconfig file and update it with these values –

[core] 
packedGitLimit = 512m 
packedGitWindowSize = 512m 
[pack] 
deltaCacheSize = 2047m 
packSizeLimit = 2047m 
windowMemory = 2047m

The various configuration attributes are –

packedGitLimit – Maximum number of bytes to map simultaneously into memory from pack files.

packedGitWindowSize – Number of bytes of a pack file to map into memory in a single mapping operation.

deltaCacheSize – The maximum memory in bytes used for caching deltas in git-pack-objects before writing them out to a pack.

packSizeLimit – Maximum size of a pack.

windowMemory – The maximum size of memory that is consumed by each thread in git-pack-objects for pack window memory when no limit is given on the command line.

You can read more about these parameters here – Git Config

Solution 3 – Compression with Repack

High compression with repack can help you in solving this issue. These compression values are indicated by 0-9 where 0 means no compression and 9 means highest.

git config --global core.compression 9 repack

Solution 4: Setting GIT-TRACE Environment Variables

If you want to debug the issue you can get the complete set of traces. All you need to do is to enable some environment variables. Run these commands –

# Linux
export GIT_TRACE_PACKET=1
export GIT_TRACE=1
export GIT_CURL_VERBOSE=1

# Windows
set GIT_TRACE_PACKET=1
set GIT_TRACE=1
set GIT_CURL_VERBOSE=1

# Powershell
$env:GIT_TRACE_PACKET=1
$env:GIT_TRACE=1
$env:GIT_CURL_VERBOSE=1

GIT_TRACE controls general traces, which don’t fit into any specific category.

GIT_TRACE_PACKET enables packet-level tracing for network operations.

GIT_CURL_VERBOSE tells Git to emit all the messages generated by CURL library. This is similar to doing curl -v on the command line.

You can learn more about git environment variables here – Git Environment Variables.

Solution 5: Others

You can try other solutions like –

Conclusion

In this article we saw few solutions to resolve git error of unexpected disconnect while reading sideband packet. This issue appears due to bad network connectivity, memory overflow, git connection protocol, compression etc. We learned about all these causes and explained solutions for each of them. After applying these, you will certainly resolve your issue.