From: eulerto (@eulerto) <noreply+eulerto@github.com>
To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>
Subject: [pgbouncer/pgbouncer] PR #1667: test: fail tests when PgBouncer exits with an unexpected code
Date: Fri, 23 Oct 2026 20:49:03 +0000
Message-ID: <gh-pgbouncer-pgbouncer-1667@github.com> (raw)
Bouncer.wait_for_exit() waited for the process but ignored its exit
code, so a non-zero exit at shutdown went unnoticed. This is the gap
behind the ThreadSanitizer job: TSan reports a detected race only via
its exit code (66) at process exit, so a race that occurs during
shutdown (after the test body has finished) would leave the job green.
Capture the exit code and fail if it is not 0 (excluding Windows). Every
stop path there goes through a signal PgBouncer catches and turns into
exit(0), or an admin SHUTDOWN that also exits 0, so a non-zero code
means PgBouncer crashed or a sanitizer flagged a problem at shutdown.
Windows is excluded because the harness stops PgBouncer with
terminate(), a hard kill that exits non-zero by design. The terminate()
does not provide an argument to pass custom exit code and it hardcodes
the exit code to 1.
This complements PR #1660. The halt_on_error option catches races
during normal operation (PgBouncer dies in the middle of a test), while
this check catches races that only surface at shutdown.
inline patch 1: test/utils.py (+20/-24 lines) download inline-1.patchdiff --git a/test/utils.py b/test/utils.pyindex 5f69c07f0b97..f78e1e21624a 100644--- a/test/utils.py+++ b/test/utils.py@@ -1004,7 +1004,6 @@ def __init__(
super().__init__("127.0.0.1", self.port_lock.port)
self.process: subprocess.Popen | None = None
- self.aprocess: asyncio.subprocess.Process | None = None
config_dir.mkdir()
self.config_dir = config_dir
self.ini_path = self.config_dir / "test.ini"
@@ -1095,17 +1094,14 @@ def version(self):
return pgbouncer_version
async def start(self):
- # Due to using a SelectorEventLoop for support with psycopg we cannot- # use asyncio subprocesses, since that eventloop does not support them.- # We fall back to regular subprocesses.- if WINDOWS:- self.process = subprocess.Popen( # noqa: ASYNC220- [*self.base_command(), "--quiet", self.ini_path], close_fds=True- )- else:- self.aprocess = await asyncio.create_subprocess_exec(- *self.base_command(), "--quiet", str(self.ini_path), close_fds=True- )+ # Launch PgBouncer with a plain subprocess.Popen rather than an asyncio+ # subprocess. On Windows the psycopg SelectorEventLoop does not support+ # asyncio subprocesses at all. Popen reaps through os.waitpid in the+ # subprocess module, independent of any event loop or Python version,+ # so wait() always returns PgBouncer's real exit code.+ self.process = subprocess.Popen( # noqa: ASYNC220+ [*self.base_command(), "--quiet", str(self.ini_path)], close_fds=True+ )
await self.wait_until_running()
async def wait_until_running(self):
@@ -1139,19 +1135,24 @@ def aadmin(self, query, **kwargs):
def running(self):
if self.process:
return self.process.poll() is None
- if self.aprocess:- return self.aprocess.returncode is None
return False
async def wait_for_exit(self):
+ returncode = None
if self.process is not None:
self.process.communicate()
- self.process.wait()- if self.aprocess is not None:- await self.aprocess.communicate()- await self.aprocess.wait()+ returncode = self.process.wait()
self.process = None
- self.aprocess = None++ # Every stop path goes through a signal (SIGQUIT/SIGTERM/ SIGINT) that+ # PgBouncer catches and turns into exit(0), or through an admin+ # SHUTDOWN that also exits 0, so a non-zero exit code means PgBouncer+ # crashed or a sanitizer reported a problem at shutdown (except on+ # Windows).+ if not WINDOWS and returncode not in (None, 0):+ raise RuntimeError(+ f"PgBouncer exited with unexpected exit code {returncode}"+ )
async def stop(self):
if not WINDOWS:
@@ -1162,9 +1163,6 @@ async def stop(self):
if self.process is not None:
self.process.terminate()
self.process.terminate()
- if self.aprocess is not None:- self.aprocess.terminate()- self.aprocess.terminate()
await self.wait_for_exit()
@@ -1178,8 +1176,6 @@ async def restart(self):
await self.start()
def send_signal(self, sig):
- if self.aprocess:- self.aprocess.send_signal(sig)
if self.process:
self.process.send_signal(sig)