From: ritojoao (@ritojoao) <noreply+ritojoao@github.com>
To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>
Subject: [pgbouncer/pgbouncer] PR #1710: Send cancel requests to the host of the cancelled backend
Date: Mon, 16 Nov 2026 12:46:37 +0000
Message-ID: <gh-pgbouncer-pgbouncer-1710@github.com> (raw)
Hi, thanks for pgbouncer. This is my attempt at #1702. Please tell me if
the approach is wrong.
With `host=A,B` and round-robin, `dns_connect()` picks the host with
`pool->rrcounter % count` and advances the counter. The login connection
used to forward a cancel therefore gets the next host, not the host of
the backend being cancelled. The cancel key is then sent to the wrong
server and the query is not cancelled.
The change: in `dns_connect()`, if `db->host` is a list, the pool is not a
peer pool, and the first entry of `waiting_cancel_req_list` has a
`canceled_server->host`, connect to that host and do not touch
`rrcounter`. A side effect is that cancel connections no longer move the
round-robin position.
Tested (2 PostgreSQL 17 servers on 127.0.0.1 and 127.0.0.2, session pool,
`pg_sleep(3)`, 6 iterations per case, own script):
- Before: with `host=127.0.0.1,127.0.0.2`, 3 of 6 cancels failed (the
query ran to the end). With `load_balance_hosts=disable` and with a
single host, 6 of 6 were cancelled.
- After: 6 of 6 cancelled in all three cases.
- `make check` in test/ (test_cancel, test_limits,
test_load_balance_hosts, test_peering): 63 passed. Whole suite: 287
passed, 8 skipped, 2 failed
(test_misc.py test_server_check_query_default_negative and
test_server_check_query_negative, connection timeout through the
proxy). The same 2 fail on the base commit without my change.
Known limits and not tested:
- If the first queued cancel leaves the queue (client disconnects) before
the login finishes, the login may serve the next cancel, which can
belong to another host. I expect this to be rare, but did not test it.
- If the cancel's host is down, the retry uses the same host again. With
`load_balance_hosts=disable` this skips the `last_connect_failed`
failover for that cancel. I think this is fine because the key is only
valid on that host.
- No new pytest test: the test setup starts one backend, and I would need
two plus a conftest change. I can add that if you want it.
- Peering, a host removed by reload, unix sockets in the list, TLS, IPv6.
- #1217 may be related (same host list, cancel path), but it has no
repro and I did not test peering, so I do not claim it.
Refs #1702.
inline patch 1: src/objects.c (+17/-1 lines) download inline-1.patchdiff --git a/src/objects.c b/src/objects.cindex 666c72d6a013..74d1133b7e58 100644--- a/src/objects.c+++ b/src/objects.c@@ -1690,9 +1690,25 @@ static void dns_connect(struct PgSocket *server)
int sa_len;
int res;
char *host_copy = NULL;
+ const char *cancel_host = NULL;++ /*+ * A server login that completes while cancel requests are waiting is+ * used to forward the first of them. The cancel key is only valid on+ * the host of the canceled server, so connect to that host instead of+ * the next one from the host list.+ */+ if (db->host && strchr(db->host, ',') && !db->peer_id) {+ PgSocket *req = first_socket(&server->pool->waiting_cancel_req_list);++ if (req && req->canceled_server && req->canceled_server->host)+ cancel_host = req->canceled_server->host;+ }
/* host list? */
- if (db->host && strchr(db->host, ',')) {+ if (cancel_host) {+ host = cancel_host;+ } else if (db->host && strchr(db->host, ',')) {
int count = 1;
int n;