mirror of
https://github.com/postgres/postgres.git
synced 2025-06-17 17:02:08 +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:
@ -4224,6 +4224,22 @@ PostgresMain(const char *dbname, const char *username)
|
||||
/* We need to allow SIGINT, etc during the initial transaction */
|
||||
sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
|
||||
|
||||
/*
|
||||
* Generate a random cancel key, if this is a backend serving a
|
||||
* connection. InitPostgres() will advertise it in shared memory.
|
||||
*/
|
||||
Assert(!MyCancelKeyValid);
|
||||
if (whereToSendOutput == DestRemote)
|
||||
{
|
||||
if (!pg_strong_random(&MyCancelKey, sizeof(int32)))
|
||||
{
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INTERNAL_ERROR),
|
||||
errmsg("could not generate random cancel key")));
|
||||
}
|
||||
MyCancelKeyValid = true;
|
||||
}
|
||||
|
||||
/*
|
||||
* General initialization.
|
||||
*
|
||||
@ -4276,6 +4292,7 @@ PostgresMain(const char *dbname, const char *username)
|
||||
{
|
||||
StringInfoData buf;
|
||||
|
||||
Assert(MyCancelKeyValid);
|
||||
pq_beginmessage(&buf, PqMsg_BackendKeyData);
|
||||
pq_sendint32(&buf, (int32) MyProcPid);
|
||||
pq_sendint32(&buf, (int32) MyCancelKey);
|
||||
|
Reference in New Issue
Block a user