From: Koshino Taiki <koshino@sraoss.co.jp>
To: pgpool-hackers@lists.postgresql.org <pgpool-hackers@lists.postgresql.org>
Cc: Emond Papegaaij <emond.papegaaij@gmail.com>
Subject: Convert pcp_exit_handler and wakeup_handler_parent to flag-only.
Date: Wed, 26 Aug 2026 09:21:30 +0000
Message-ID: <OS9P286MB6486FF7EC3D412F5D9B2E45F94AE2@OS9P286MB6486.JPNP286.PROD.OUTLOOK.COM> (raw)
I have reviewed the patch(027-pcp-exit-handler-flag-only) from Emond.
It has also passed all regression tests.
The signal handlers in the PCP parent process directly accessed the
pcp_worker_children list and performed child-process cleanup. These
operations are not async-signal-safe and can modify or traverse the list
while the main loop is updating it.
In particular, pcp_exit_handler() can run while the main loop is adding
or removing worker entries. This can cause the handler to traverse a
partially modified list, crash, or send a signal to a reused PID.
wakeup_handler_parent() has the same risk when forwarding SIGUSR2 to
worker processes.
The handlers also call operations such as exit(), waitpid(), close(),
and Pgpool List functions from signal context.
Changes:
Flag-only Signal Handlers: Make pcp_exit_handler() and
wakeup_handler_parent() only set volatile sig_atomic_t flags and return.
Main-loop Processing: Process pending exit and wakeup requests from the
main loop, where the worker list can be accessed safely.
Direct Restart Cleanup: Update the PCP restart path to invoke the cleanup
function directly from normal process context.
Also, regression tests passed after applying this together with the other
similar signal handler fixes, 026-close-idle-connection-flag-only and029-pcp-worker-die-flag-only.
However, 028-lifecheck-exit-handler-flag-only is failing due to a timeout
in the 004 watchdog test case, so 028 has not been posted to this ML yet.
Thank you Emond.
Taiki Koshino<koshino@sraoss.co.jp>
SRA OSS K.K.
TEL: 03-5979-2701 FAX: 03-5979-2702
URL: https://www.sraoss.co.jp/Attachments:v1-0001-Convert-pcp_exit_handler-and-wakeup_handler_paren.patch (application/octet-stream, 4.5K) download
From b6e34d7d89d92637a92842dc7d16d43fb21cfe8b Mon Sep 17 00:00:00 2001
From: Taiki Koshino <koshino@sraoss.co.jp>
Date: Thu, 20 Aug 2026 08:31:21 +0900
Subject: [PATCH v1] Convert pcp_exit_handler and wakeup_handler_parent to
flag-only.
The signal handlers in the PCP parent process directly accessed the
pcp_worker_children list and performed child-process cleanup. These
operations are not async-signal-safe and can modify or traverse the list
while the main loop is updating it.
In particular, pcp_exit_handler() can run while the main loop is adding
or removing worker entries. This can cause the handler to traverse a
partially modified list, crash, or send a signal to a reused PID.
wakeup_handler_parent() has the same risk when forwarding SIGUSR2 to
worker processes.
The handlers also call operations such as exit(), waitpid(), close(),
and Pgpool List functions from signal context.
Changes:
Flag-only Signal Handlers: Make pcp_exit_handler() and
wakeup_handler_parent() only set volatile sig_atomic_t flags and return.
Main-loop Processing: Process pending exit and wakeup requests from the
main loop, where the worker list can be accessed safely.
Direct Restart Cleanup: Update the PCP restart path to invoke the cleanup
function directly from normal process context.
Reported-by: Emond Papegaaij <emond.papegaaij@gmail.com>
Reported-by: Claude Code
Author: Taiki Koshino <koshino@sraoss.co.jp>
Discussion:
Backpatch-through: v4.3
---
src/pcp_con/pcp_child.c | 58 ++++++++++++++++++++++++++++++++++-------
1 file changed, 49 insertions(+), 9 deletions(-)
diff --git a/src/pcp_con/pcp_child.c b/src/pcp_con/pcp_child.cindex 23d233146..687b7ec63 100644--- a/src/pcp_con/pcp_child.c+++ b/src/pcp_con/pcp_child.c@@ -74,6 +74,9 @@ static volatile sig_atomic_t pcp_got_sighup = 0;
static volatile sig_atomic_t pcp_restart_request = 0;
List *pcp_worker_children = NULL;
static volatile sig_atomic_t sigchld_request = 0;
+static volatile sig_atomic_t pcp_exit_request = 0;+static volatile sig_atomic_t pcp_exit_request_sig = 0;+static volatile sig_atomic_t pcp_wakeup_request = 0;
static RETSIGTYPE pcp_exit_handler(int sig);
static RETSIGTYPE wakeup_handler_parent(int sig);
@@ -86,6 +89,7 @@ static void start_pcp_command_processor_process(int port, int *fds);
static void pcp_child_will_die(int code, Datum arg);
static void pcp_kill_all_children(int sig);
static void reaper(void);
+static void process_pcp_exit_request(void);
static bool pcp_unix_fds_not_isset(int *fds, int num_pcp_fds, fd_set *opt);
@@ -95,10 +99,21 @@ static bool pcp_unix_fds_not_isset(int *fds, int num_pcp_fds, fd_set *opt);
{ \
reaper(); \
} \
+ if (pcp_wakeup_request) \+ { \+ pcp_wakeup_request = 0; \+ pcp_kill_all_children(SIGUSR2); \+ } \+ if (pcp_exit_request) \+ { \+ process_pcp_exit_request(); \+ } \
if (pcp_restart_request) \
{ \
ereport(LOG,(errmsg("restart request received in pcp child process"))); \
- pcp_exit_handler(SIGTERM); \+ pcp_exit_request = 1; \+ pcp_exit_request_sig = SIGTERM; \+ process_pcp_exit_request(); \
} \
} while (0)
@@ -446,14 +461,47 @@ reaper(void)
}
}
+/*+ * Async-signal-safe exit handler: just record the request and the+ * triggering signal number. The pcp_worker_children list is mutated by+ * non-handler code (lappend_int, list_delete_int) without blocking these+ * signals, so traversal must happen at a safe point in the main loop via+ * process_pcp_exit_request().+ */
static RETSIGTYPE
pcp_exit_handler(int sig)
{
+ pcp_exit_request_sig = sig;+ pcp_exit_request = 1;+}++/*+ * Wakeup signal handler for pcp parent process. Async-signal-safe: just+ * record the request; the main loop forwards SIGUSR2 to workers at a safe+ * point where the pcp_worker_children list is not being mutated.+ */+static RETSIGTYPE+wakeup_handler_parent(int sig)+{+ pcp_wakeup_request = 1;+}++/*+ * Process a pending exit request set by pcp_exit_handler(). Runs in the+ * main loop, where it is safe to walk pcp_worker_children.+ */+static void+process_pcp_exit_request(void)+{+ int sig;
pid_t wpid;
ListCell *lc;
POOL_SETMASK(&AuthBlockSig);
+ sig = pcp_exit_request_sig;+ pcp_exit_request = 0;+
pcp_kill_all_children(sig);
if (sig == SIGTERM) /* smart shutdown */
@@ -481,14 +529,6 @@ pcp_exit_handler(int sig)
exit(0);
}
-/* Wakeup signal handler for pcp parent process */-static RETSIGTYPE-wakeup_handler_parent(int sig)-{- /* forward wakeup signal to all children */- pcp_kill_all_children(SIGUSR2);-}-
static RETSIGTYPE
restart_handler(int sig)
{
--
2.52.0
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgpool-hackers@postgresql.org
Cc: koshino@sraoss.co.jp, emond.papegaaij@gmail.com
Subject: Re: Convert pcp_exit_handler and wakeup_handler_parent to flag-only.
In-Reply-To: <OS9P286MB6486FF7EC3D412F5D9B2E45F94AE2@OS9P286MB6486.JPNP286.PROD.OUTLOOK.COM>
References: <OS9P286MB6486FF7EC3D412F5D9B2E45F94AE2@OS9P286MB6486.JPNP286.PROD.OUTLOOK.COM>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox