Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vybCv-000FOO-10 for pgsql-hackers@arkaria.postgresql.org; Fri, 06 Mar 2026 19:51:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vybCt-006qVJ-2M for pgsql-hackers@arkaria.postgresql.org; Fri, 06 Mar 2026 19:51:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vybCt-006qVB-1D for pgsql-hackers@lists.postgresql.org; Fri, 06 Mar 2026 19:51:31 +0000 Received: from lahtoruutu.iki.fi ([185.185.170.37]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vybCr-00000001Ed7-2LgO for pgsql-hackers@lists.postgresql.org; Fri, 06 Mar 2026 19:51:31 +0000 Received: from [10.0.2.15] (unknown [130.41.208.2]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4fSH8q2q23z49Q6d; Fri, 06 Mar 2026 21:51:23 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1772826684; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sPAtNaOVxcPrDtCycj2YT8Rg3F+tfNoMtUmltIVWAtI=; b=ju+I8+MgCndL4TljaxkOJnZi1n7+bw4oYMeOWjweNQrGnC3CAHh+0sQOQrv4bUz/FZBHID i232TmO8/60sRT8w5Ij239R7SHkC0oOiWqvZMOJ1MKA3urT+j5SK3cx+1JDoUAg2CX3iZr QOpFdMj7MybZfvOVIuP8v7Ge6tYluIb4qcdZLv4CFjZbz0pb3tDal4XZAIZUoFZzd3ssT0 FlkljyeKtOAjsxGmuwVyicYAoM6enrPYEcsf1mSnc+DhvKMW1IB+SaK8AKMCfSQIRrq66m NRa9UN3E3e8VLnqgqvRnkdw5C5M1QT15LGZt3Gqv/yMJeDVczyw0e+SS2G2Aog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1772826684; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sPAtNaOVxcPrDtCycj2YT8Rg3F+tfNoMtUmltIVWAtI=; b=df9eJ+P/VyoxuPWu0WV6AjLvlrYAq8rucXUvcPNeTC7YLdDS2axUgSQwYR4TforEc34Az4 cFl0lsuzYp01v40sI8ULvs9Q5zY+nML/1mg6w9LUA4H54p4smmZikAY/7OY/WEUFNgyup4 1AxI4KljFXfWyUBt4u8Vkuf9mjjDOJJeuUL2qLj4T2qXJ2q8WOfZEKhWcRVQD8OAJr9V2a +6AGfEgfGEZdpY4tzCI7BizvrTFHj7AjHZq9FlZw18LxoCf0uvBJCDZBL6+Dgphr57NCFx lfEQORvzsteAygyxCkTqR7klKP4fEd/Y45T3d8eKAqnTw/3Axo6DZhpI4RmxWg== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Seal: i=1; a=rsa-sha256; d=iki.fi; s=lahtoruutu; cv=none; t=1772826684; b=Ono0osLyOItQlb7Zz1ZAkZeid+TfsUkZSmmhhK2TOoA40a5V+J7yoiMujsC8aAgG93i0FZ HCOCRyeXqs3OoQ3Dbssd5WmsX2WSCbM1cUkR/WIk1Uh0/KRtAWREk9Gs6943A3iDd/jA0H TEiVTTakDvF57jWmwqf/e5T4855IGoyBXRnksq/jyD5sVOz3Ewm9ey8oY/fq/nDfG3+F4e LfaM6RDDQUHc/t/31dpd8q2LKc6rwY35vwWUpPKYpWBTLeeo7bLQRSmvL9POPnH1CN1K27 72OAOZMf7MzoHeVoyrHg4POO5qtv/n//OFhKL0UbhckZ3120JZHjZx98lsWXVQ== Message-ID: <9d7ba3ac-d660-483e-8f68-9096a2464e90@iki.fi> Date: Fri, 6 Mar 2026 21:51:22 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Don't use the deprecated and insecure PQcancel in our frontend tools anymore To: Jelte Fennema-Nio , PostgreSQL Hackers , Alvaro Herrera , Jacob Champion References: <88dfe280-ba29-4943-95b8-63abc9f3f771@iki.fi> Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 06/03/2026 04:12, Jelte Fennema-Nio wrote: > On Thu Mar 5, 2026 at 7:30 PM CET, Heikki Linnakangas wrote: >> It took me a while to get the big picture of how this works. cancel.c >> could use some high-level comments explaining how to use the facility; >> it's a real mixed bag right now. > > Attached is a version with a bunch more comments. I agree this cancel > logic is hard to understand without them. It took me quite a while to > understand it myself. (I don't think the code got any harder to > understand with these changes though, the exact same complexity was > already there for Windows. But I agree more commends are good.) Thanks. I agree it was complicated before these patches. >> This is racy, if the cancellation thread doesn't immediately process >> the wakeup. For example, because it's still busy processing a previous >> wakeup, because there's a network hiccup or something. By the time the >> cancellation thread runs, the main thread might already be running a >> different query than it was when the user hit CTRL-C. > > I now noted this in one of the new comments. I don't think there's a way > around this race condition entirely. It's simply a limitation of our > cancel protocol (because it's impossible to specify which query on a > connection should be cancelled). That's true, but I still wonder if this could make it much worse. > In theory we could reduce the window for the race, by having all > frontend tools use async connections and have the main thread wait for > either the self-pipe or a cancel. That way it would be more similar to > the previous signal code in behaviour. That's a much bigger lift though, > i.e. all PQexec and PQgetResult calls would need to be modified. My > proposed change doesn't require changing the callsites at all. Yeah, it does have that advantage.. One simple thing we could is to remember the "generation" in the signal handler, and store it in another global variable ("cancelledGeneration" or such). In the cancel thread, check that the generation matches; otherwise the thread is about to send a cancellation to a query that already finished, and should not send it. I worry how this behaves if establishing the cancel connection gets stuck for a long time. Because of a network hiccup, for example. That's also not a new problem though; it's perhaps even worse today, if the signal handler gets stuck for a long time, trying to establish the connection. Still, would be good to do some testing with a bad network. - Heikki