mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Move cancel key generation to after forking the backend
Move responsibility of generating the cancel key to the backend process. The cancel key is now generated after forking, and the backend advertises it in the ProcSignal array. When a cancel request arrives, the backend handling it scans the ProcSignal array to find the target pid and cancel key. This is similar to how this previously worked in the EXEC_BACKEND case with the ShmemBackendArray, just reusing the ProcSignal array. One notable change is that we no longer generate cancellation keys for non-backend processes. We generated them before just to prevent a malicious user from canceling them; the keys for non-backend processes were never actually given to anyone. There is now an explicit flag indicating whether a process has a valid key or not. I wrote this originally in preparation for supporting longer cancel keys, but it's a nice cleanup on its own. Reviewed-by: Jelte Fennema-Nio Discussion: https://www.postgresql.org/message-id/508d0505-8b7a-4864-a681-e7e5edfe32aa@iki.fi
This commit is contained in:
@ -29,6 +29,7 @@
|
||||
#include "replication/walsender.h"
|
||||
#include "storage/fd.h"
|
||||
#include "storage/ipc.h"
|
||||
#include "storage/procsignal.h"
|
||||
#include "storage/proc.h"
|
||||
#include "tcop/backend_startup.h"
|
||||
#include "tcop/tcopprot.h"
|
||||
@ -541,6 +542,11 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
|
||||
|
||||
if (proto == CANCEL_REQUEST_CODE)
|
||||
{
|
||||
/*
|
||||
* The client has sent a cancel request packet, not a normal
|
||||
* start-a-new-connection packet. Perform the necessary processing.
|
||||
* Nothing is sent back to the client.
|
||||
*/
|
||||
CancelRequestPacket *canc;
|
||||
int backendPID;
|
||||
int32 cancelAuthCode;
|
||||
@ -556,7 +562,8 @@ ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done)
|
||||
backendPID = (int) pg_ntoh32(canc->backendPID);
|
||||
cancelAuthCode = (int32) pg_ntoh32(canc->cancelAuthCode);
|
||||
|
||||
processCancelRequest(backendPID, cancelAuthCode);
|
||||
if (backendPID != 0)
|
||||
SendCancelRequest(backendPID, cancelAuthCode);
|
||||
/* Not really an error, but we don't want to proceed further */
|
||||
return STATUS_ERROR;
|
||||
}
|
||||
|
Reference in New Issue
Block a user