public inbox for [email protected]  
help / color / mirror / Atom feed
Close listening socokets before forking
2+ messages / 1 participants
[nested] [flat]

* Close listening socokets before forking
@ 2026-03-02 01:00 Tatsuo Ishii <[email protected]>
  2026-03-09 09:20 ` Re: Close listening socokets before forking Tatsuo Ishii <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Tatsuo Ishii @ 2026-03-02 01:00 UTC (permalink / raw)
  To: [email protected]

Currently when pgpool main process forks sub processes (child process
- user session process, pcp_main process, health check process,
streaming replication check process and life check process), they
inherits pgpool and pcp listening sockets. However some of processes
do not need those sockets:

- child process - pcp sockets are unnecessary
- pcp main process - pgpool sockets are unnecessary
- health check, streaming replication check and life check process -
  pgpool and pcp sockets are unnecessary

It could be potential problem when those process go down. Since they
may keep the listening sockets for a while, which prevents next pgpool
starting up from binding those ports.

Attached patch closes those unnecessary sockets after forking. For
this purpose new function close_listening_sockets() is introduced.

Best regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp


Attachments:

  [application/octet-stream] close_socks.patch (4.2K, 2-close_socks.patch)
  download | inline diff:
diff --git a/src/include/pool.h b/src/include/pool.h
index ea6f87e12..65907dcf1 100644
--- a/src/include/pool.h
+++ b/src/include/pool.h
@@ -4,8 +4,8 @@
  * pgpool: a language independent connection pool server for PostgreSQL
  * written by Tatsuo Ishii
  *
- * Portions Copyright (c) 2003-2025	PgPool Global Development Group
- * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
+ * Portions Copyright (c) 2003-2026	PgPool Global Development Group
+ * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * Permission to use, copy, modify, and distribute this software and
@@ -709,6 +709,15 @@ extern bool pool_acquire_follow_primary_lock(bool block, bool remote_reques);
 extern void pool_release_follow_primary_lock(bool remote_reques);
 extern void pool_signal_logrotate(void);
 
+/*
+ * Close listening sockets
+ * following defines are for "kind" param.
+ */
+#define POOL_CLOSE_PGPOOL_LISTENING_SOCKS	0x01	/* pgpool sockets */
+#define POOL_CLOSE_PCP_LISTENING_SOCKS		0x02	/* pcp sockets */
+extern void close_listening_sockets(int kind);
+
+
 /* strlcpy.c */
 #ifndef HAVE_STRLCPY
 extern size_t strlcpy(char *dst, const char *src, size_t siz);
diff --git a/src/main/pgpool_main.c b/src/main/pgpool_main.c
index fa05e15e7..bf7c452e2 100644
--- a/src/main/pgpool_main.c
+++ b/src/main/pgpool_main.c
@@ -212,6 +212,7 @@ static struct sockaddr_un *pcp_un_addrs;	/* unix domain socket path for PCP */
 ProcessInfo *process_info = NULL;	/* Per child info table on shmem */
 volatile User1SignalSlot *user1SignalSlot = NULL;	/* User 1 signal slot on
 													 * shmem */
+
 int			current_child_process_count;
 
 /*
@@ -815,6 +816,9 @@ pcp_fork_a_child(int *fds, char *pcp_conf_file)
 		on_exit_reset();
 		SetProcessGlobalVariables(PT_PCP);
 
+		/* close pgpool listening sockets */
+		close_listening_sockets(POOL_CLOSE_PGPOOL_LISTENING_SOCKS);
+
 		close(pipe_fds[0]);
 		close(pipe_fds[1]);
 
@@ -862,6 +866,9 @@ fork_a_child(int *fds, int id)
 
 		SetProcessGlobalVariables(PT_CHILD);
 
+		/* close pcp listening sockets */
+		close_listening_sockets(POOL_CLOSE_PCP_LISTENING_SOCKS);
+
 		/* call child main */
 		POOL_SETMASK(&UnBlockSig);
 		health_check_timer_expired = 0;
@@ -909,6 +916,14 @@ worker_fork_a_child(ProcessType type, void (*func) (void *), void *params)
 
 		SetProcessGlobalVariables(type);
 
+		/*
+		 * Close pgpool listening sockets and pcp listening sockets if they
+		 * are already setup. They are not ncessary for woker process (health
+		 * check and streaming replication check process).
+		 */
+		close_listening_sockets(POOL_CLOSE_PGPOOL_LISTENING_SOCKS |
+								POOL_CLOSE_PCP_LISTENING_SOCKS);
+
 		ereport(LOG,
 				(errmsg("process started")));
 
@@ -5227,3 +5242,25 @@ select_victim_processes(int *process_info_idxs, int count)
 	}
 	return selected_count;
 }
+
+/*
+ * Close all listening sockets
+ *
+ * kind bitmap specifies which listening sockets to be closed.
+ */
+void
+close_listening_sockets(int kind)
+{
+	int			i;
+
+	if (kind & POOL_CLOSE_PGPOOL_LISTENING_SOCKS && fds != NULL)
+	{
+		for (i = 0; fds[i] > 0; i++)
+			close(fds[i]);
+	}
+	if (kind & POOL_CLOSE_PCP_LISTENING_SOCKS && pcp_fds != NULL)
+	{
+		for (i = 0; pcp_fds[i] > 0; i++)
+			close(pcp_fds[i]);
+	}
+}
diff --git a/src/watchdog/wd_lifecheck.c b/src/watchdog/wd_lifecheck.c
index 86caf9b4b..a6958a395 100644
--- a/src/watchdog/wd_lifecheck.c
+++ b/src/watchdog/wd_lifecheck.c
@@ -6,7 +6,7 @@
  * pgpool: a language independent connection pool server for PostgreSQL
  * written by Tatsuo Ishii
  *
- * Copyright (c) 2003-2025	PgPool Global Development Group
+ * Copyright (c) 2003-2026	PgPool Global Development Group
  *
  * Permission to use, copy, modify, and distribute this software and
  * its documentation for any purpose and without fee is hereby
@@ -367,6 +367,10 @@ fork_lifecheck_child(void)
 		on_exit_reset();
 		SetProcessGlobalVariables(PT_LIFECHECK);
 
+		/* close pgpool/pcp listening sockets */
+		close_listening_sockets(POOL_CLOSE_PGPOOL_LISTENING_SOCKS |
+								POOL_CLOSE_PCP_LISTENING_SOCKS);
+
 		/* call lifecheck child main */
 		POOL_SETMASK(&UnBlockSig);
 		lifecheck_main();


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Close listening socokets before forking
  2026-03-02 01:00 Close listening socokets before forking Tatsuo Ishii <[email protected]>
@ 2026-03-09 09:20 ` Tatsuo Ishii <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Tatsuo Ishii @ 2026-03-09 09:20 UTC (permalink / raw)
  To: [email protected]

> Currently when pgpool main process forks sub processes (child process
> - user session process, pcp_main process, health check process,
> streaming replication check process and life check process), they
> inherits pgpool and pcp listening sockets. However some of processes
> do not need those sockets:
> 
> - child process - pcp sockets are unnecessary
> - pcp main process - pgpool sockets are unnecessary
> - health check, streaming replication check and life check process -
>   pgpool and pcp sockets are unnecessary
> 
> It could be potential problem when those process go down. Since they
> may keep the listening sockets for a while, which prevents next pgpool
> starting up from binding those ports.
> 
> Attached patch closes those unnecessary sockets after forking. For
> this purpose new function close_listening_sockets() is introduced.

Pushed down to v4.4 (v4.3 was hard to port the patch).

Best regards,
--
Tatsuo Ishii
SRA OSS K.K.
English: http://www.sraoss.co.jp/index_en/
Japanese:http://www.sraoss.co.jp






^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2026-03-09 09:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-03-02 01:00 Close listening socokets before forking Tatsuo Ishii <[email protected]>
2026-03-09 09:20 ` Tatsuo Ishii <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox