diff options
author | Heikki Linnakangas | 2024-07-29 12:37:48 +0000 |
---|---|---|
committer | Heikki Linnakangas | 2024-07-29 12:37:48 +0000 |
commit | 9d9b9d46f3c509c722ebbf2a1e7dc6296a6c711d (patch) | |
tree | 450cf30fdaca9dd1b5336179f2bb590e397fb9f0 /src/backend/tcop/backend_startup.c | |
parent | 19de089cdc23373e2f36916017a1e23e8ff4c2f8 (diff) |
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/[email protected]
Diffstat (limited to 'src/backend/tcop/backend_startup.c')
-rw-r--r-- | src/backend/tcop/backend_startup.c | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/src/backend/tcop/backend_startup.c b/src/backend/tcop/backend_startup.c index b840d95e4d1..9ad60a6dc7f 100644 --- a/src/backend/tcop/backend_startup.c +++ b/src/backend/tcop/backend_startup.c @@ -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; } |