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 1vyDTD-00HXaI-0T for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Mar 2026 18:30:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vyDTB-000kIU-1v for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Mar 2026 18:30:46 +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 1vyDTB-000kIL-0h for pgsql-hackers@lists.postgresql.org; Thu, 05 Mar 2026 18:30:45 +0000 Received: from lahtoruutu.iki.fi ([2a0b:5c81:1c1::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 1vyDT5-000000010hQ-3iLY for pgsql-hackers@lists.postgresql.org; Thu, 05 Mar 2026 18:30:45 +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 4fRdQ35gh6z49Q6d; Thu, 05 Mar 2026 20:30:35 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1772735436; 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=NBw3NI521/s5Ba+HtwSmvi1IsJlmMmScuVI3QxRobyA=; b=lzn3XHRkFPv6x5rW9SP7xpP1td+DDOR17tTKfZ2b8ftyQUS6fwoiY7hdgvFIamtUzGMPjE FKmQuNFI7bkTyoUzPQF1myuJptSg13AV/JfGz8b727NohRQjWOfhR0cn8NZ31zgq2R7272 5g3sPxlTSHRSnPT9gLuEut01HoVmWCs/YyYnAf9xTBwRgEnc2T6n+a9XCVvZvgJVSygzz8 VZSamQsYeysuHWJHMRx8KOY+rR3yMPPu23HBTnXJhZ6ZSTtnHf2S6RhQBR2zHrJsh4MnA7 zFF+vrxEcjHZ80uHDN0/i/Ga6Fls1eZuPS0jOWU0kVxAr51n/Czsri4tfWklBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1772735436; 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=NBw3NI521/s5Ba+HtwSmvi1IsJlmMmScuVI3QxRobyA=; b=abSNCRZkoeZT4MmIIQLWJxlT25A7K1c1mJj+ZP46kP3K+1iRq0WyYLvcGh+aWBTD7kIedd lfu8MhP8x47wY9gvg4fzfICbIs6RHpuJfSml4QegNxJCGqj+Mi/Wx05yXDfVWriu2w31kH TKzx6RtdLD5of0nH6XXIrP3q630wTgI1dD1i9YV1oWKGfGOW05hR3J1dpp1HHsU25U8clx aQiVXVascy0GylTtyAr2VKjHvE0c7UuBUykHLL1zA342RX1qEUcs9zCm+amMrhl5CG3XmA GsiABUmzzAaHRg62b1UmmeU0CHzIxpArgRlf5cbFgxpLV49j6WgL+eFlW10s+w== 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=1772735436; b=T/0bEARaflQgbnZyslWNvKp0BBZK1p3x51dU5UIzhVeg7ueHxnt7Wck2q1e09FMSxtJFrC M3ZwYfIO4v9P5WFMfIrYfNoLCozbX+7oOULrrFTJmGV51W3XD4ZwabFKQh3LXzriQljEb8 LW06pBEJCC2+FZyJnX0jzUtoswbt0jEgkPcS0oApJ0j96CxmB54SasEtTSAcIOB4jTnTq+ AqVxZ+vqYKxDtwVPmBrWKzFdrLLZxGUHJHFcHUKAYQWHD77+rIFe5TReaFjcEV3aySrJCf pL5Mshb0o3CLBUo83CCnS3qn5sOBpZxx/Vut6lPu5SU1epq75vw4czW8NX0R4g== Message-ID: <88dfe280-ba29-4943-95b8-63abc9f3f771@iki.fi> Date: Thu, 5 Mar 2026 20:30:29 +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: 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 08/02/2026 21:05, Jelte Fennema-Nio wrote: > On Sun Dec 14, 2025 at 3:40 PM CET, Jelte Fennema-Nio wrote: >> A bunch of frontend tools, including psql, still used PQcancel to send >> cancel requests to the server. That function is insecure, because it >> does not use encryption to send the cancel request. This starts using >> the new cancellation APIs (introduced in 61461a300) for all these >> frontend tools. > > Small update. Split up the fe_utils and pg_dump changes into separate > commits, to make patches easier to review. Also use non-blocking writes > to the self-pipe from the signal handler to avoid potential deadlocks > (extremely unlikely for such blocks to occur, but better safe than sorry). Had a brief look at this: 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. The SIGINT handler now does three things: - Set CancelRequested global variable, - call callback if set, and - wake up the cancel thread to send the cancel message, if cancel connection is set. There's no high-level overview documentation or comments on how those three mechanism work or interact. It took me a while to understand that they are really separate, alternative ways to handle SIGINT, all mashed into the same signal handler function. At first read, I thought they're somehow part of the one same mechanism. The cancelConn mechanism is a global variable, which means that it can only be used with one connection in the process. That's OK with the current callers, but seems short-sighted. What if we wanted to use it for pgbench, for example, which uses multiple threads and connections? Or if we changed pg_dump to use multiple threads, like you also suggested as a possible follow-up. The "self-pipe trick" usually refers to interrupting the main thread from select(); this uses it to wake up the other, separate cancellation thread. That's fine, but again it took me a while to understand that that's what it does. Comments! 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. - Heikki