<p>I have configured Pgbouncer as a sidecar pattern in one of my pods in Azure Kubernetes based on <a href="https://hub.docker.com/_/microsoft-azure-oss-db-tools-pgbouncer-sidecar" rel="nofollow noreferrer">Azure Oss Db Tools Pgbouncer Sidecar</a> documentation. It has the following container lifecycle hook:</p>
<pre class="lang-sh prettyprint-override"><code>lifecycle:
  preStop:
    exec:
      command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;killall -INT pgbouncer &amp;&amp; sleep 120&quot;]
</code></pre>
<p>I believe the intended purpose of this command is to wait 120 seconds until any running query finishes.</p>
<hr />
<p>To understand what it does, I opened two interactive shells inside the Pgbouncer container. In the first shell, I executed the <code>killall</code> command and in the second shell, I executed <code>ps</code> command multiple times.</p>
<h3 id="first-shell-pr7f">First shell:</h3>
<pre class="lang-sh prettyprint-override"><code>/ $ ps
PID   USER     TIME  COMMAND
    1 postgres  0:00 /usr/bin/pgbouncer /etc/pgbouncer/pgbouncer.ini
    6 postgres  0:00 /bin/sh
   30 postgres  0:00 ps
/ $
/ $
/ $ killall -INT pgbouncer &amp;&amp; sleep 120
</code></pre>
<h3 id="second-shell-r6xk">Second shell:</h3>
<pre class="lang-sh prettyprint-override"><code>/ $ ps
PID   USER     TIME  COMMAND
    1 postgres  0:00 /usr/bin/pgbouncer /etc/pgbouncer/pgbouncer.ini
    6 postgres  0:00 /bin/sh
   33 postgres  0:00 /bin/sh
   40 postgres  0:00 sleep 120
   41 postgres  0:00 ps
/ $
/ $
/ $ ps
PID   USER     TIME  COMMAND
    1 postgres  0:00 /usr/bin/pgbouncer /etc/pgbouncer/pgbouncer.ini
    6 postgres  0:00 /bin/sh
   33 postgres  0:00 /bin/sh
   42 postgres  0:00 ps
</code></pre>
<p>After 120 seconds, Pgbouncer main process is still running (See the output from the second shell). I thought this should have terminated both my terminal sessions since it was supposed to kill Pgbouncer process (<code>PID</code> = <code>1</code>) and stop the container.</p>
<hr />
<p>If I try to kill using the below command:</p>
<pre class="lang-sh prettyprint-override"><code>/ $ kill 1
/ $ command terminated with exit code 137
</code></pre>
<p>I see that both my terminal sessions are immediately terminated and the container is stopped.</p>
<hr />
<p>I want to understand whether we really need this lifecycle hook since it is not properly working? Or did I make any mistake trying to understand what it does?</p>
<p>Thanks for the help! 🙏</p>

Eranga Heshan
<p>There is a difference here. The <code>killall -INT</code> sends INT signal while <code>kill</code> sends TERM signal if no signal is specified. You can try again with <code>kill -INT 1</code> to see whether it gets the same behavior. I think the <code>pgbouncer</code> process is also catching INT.</p>
<p>Here is reference to the <a href="https://github.com/pgbouncer/pgbouncer/blob/cfbb8a3ae7a09968e9cd12f4afb907496d72d679/src/main.c" rel="nofollow noreferrer">site</a>:</p>
<pre class="lang-c prettyprint-override"><code>int cf_shutdown; /* 1 - wait for queries to finish, 2 - shutdown immediately */

...

static void handle_sigterm(evutil_socket_t sock, short flags, void *arg)
{
    log_info(&quot;got SIGTERM, fast exit&quot;);
    /* pidfile cleanup happens via atexit() */
    exit(1);
}
    
static void handle_sigint(evutil_socket_t sock, short flags, void *arg)
{
    log_info(&quot;got SIGINT, shutting down&quot;);
    sd_notify(0, &quot;STOPPING=1&quot;);
    if (cf_reboot)
        die(&quot;takeover was in progress, going down immediately&quot;);
    if (cf_pause_mode == P_SUSPEND)
        die(&quot;suspend was in progress, going down immediately&quot;);
    cf_pause_mode = P_PAUSE;
    cf_shutdown = 1;
}
</code></pre>
<p>Pgbouncer stops processing any more queries when the INT signal is given while the TERM signal will terminate the process immediately.</p>
