<metaname="description"content="An increasing number of zombies processes are created by Gitea because it only kills its direct children on timeout."/>
<metaproperty="og:title"content="[diagnostic] Zombies created by Gitea | Gna!: Managed Gitea Hosting "/>
<metaproperty="og:description"content="An increasing number of zombies processes are created by Gitea because it only kills its direct children on timeout."/>
<meta
property="og:site_name"
content="[diagnostic] Zombies created by Gitea | Gna!: Managed Gitea Hosting "
<p>The first <ahref="https://github.com/go-gitea/gitea/issues/3242">issue about zombie processes</a> created by Gitea was reported in 2017 and <ahref="https://github.com/go-gitea/gitea/issues/13987">resurfaced</a> on a <ahref="https://github.com/go-gitea/gitea/issues/19077">regular basis</a>. Although it does not look pretty, zombie processes are leftovers that do not consume resources and never caused any kind of harm. Here is one scenario that will create a zombie:</p>
<ul>
<li>Gitea updates a mirror by spawning the process <code>git remote update</code></li>
<li><code>git remote update</code> spawns yet another process, <code>git fetch</code></li>
<li><code>git fetch</code> is stuck, for instance because of network problems, and Gitea eventually times out</li>
<li>Gitea kill the process <code>git remote update</code></li>
<li>When killed <code>git remote update</code> does not kill its own child and <code>git fetch</code> becomes an orphaned process which keeps running</li>
<li>When <code>git fetch</code> eventually completes it becomes a zombie because its original parent is no longer around to wait on it</li>
</ul>
<h3id="pid-1-process-and-waiting-on-orphans">PID 1 process and waiting on orphans<aclass="zola-anchor"href="#pid-1-process-and-waiting-on-orphans"aria-label="Anchor link for: pid-1-process-and-waiting-on-orphans"
><spanclass="anchor-icon">#</span></a
>
</h3>
<p>This scenario is not unique to Gitea and it is such a common pattern that safeguards have been implemented to mitigate the proliferation of zombies. Orphaned process are automatically attached to the process with PID 1, which is expected to wait on every process, whether it created them or not. When Gitea is installed from binary on GNU/Linux this is <code>/bin/init</code> and when Gitea runs from the <ahref="https://github.com/go-gitea/gitea/blob/6171ea7d318c0ca8714bc6efd6a97ea4b495eb6d/Dockerfile">default docker image</a> this is <code>s6</code>: they will both wait on orphaned processes and there won't be any zombies.</p>
<h3id="what-if-gitea-is-the-only-running-process">What if Gitea is the only running process?<aclass="zola-anchor"href="#what-if-gitea-is-the-only-running-process"aria-label="Anchor link for: what-if-gitea-is-the-only-running-process"
><spanclass="anchor-icon">#</span></a
>
</h3>
<p>But when Gitea runs from the <ahref="https://github.com/go-gitea/gitea/blob/6171ea7d318c0ca8714bc6efd6a97ea4b495eb6d/Dockerfile.rootless">rootless docker image</a>, Gitea is the only process running in the container. Orphaned processes will have Gitea as a parent but will not wait on them and they will stay in a zombie state forever. To reproduce this problem in a minimal way:</p>
</span><span> 0 1 gitea /usr/local/bin/gitea -c /etc/gitea/app.ini web
</span><span> 1 94 sleep [sleep]
</span><span> 1 99 sleep [sleep]
</span><span> 1 111 sleep [sleep]
</span><span> 1 165 sleep [sleep]
</span></code></pre>
<h3id="killing-a-child-process-and-all-its-children">Killing a child process and all its children<aclass="zola-anchor"href="#killing-a-child-process-and-all-its-children"aria-label="Anchor link for: killing-a-child-process-and-all-its-children"
><spanclass="anchor-icon">#</span></a
>
</h3>
<p>There should be no need for an admin running Gitea to worry about those gory details, it should be taken care of regardless of the environment Gitea runs in. Fortunately there is a very simple way to avoid the creation of zombies by ensuring that all Gitea child process are <ahref="https://en.wikipedia.org/wiki/Process_group">process group leaders</a>. In a nutshell it means that when the child is killed all its children and grand children are also killed.</p>