Assert there is no duplicated exit callbacks

Started by Qingqing Zhouover 10 years ago4 messages
#1Qingqing Zhou
zhouqq.postgres@gmail.com
1 attachment(s)

Attached is a patch to assert there is no duplicated exit callbacks.

Along the way, I downgrade the runtime "enough room" check to an
assertion: the callbacks are registered in pretty fixed initialization
code path, thus assertion is good enough to prevent overlook there. If
we don't agree with this, then the duplication check shall also bump
to runtime checks.

Regards,
Qingqing

Attachments:

ipc.difftext/plain; charset=UTF-16LE; name=ipc.diffDownload
diff --git a/src/backend/storage/ipc/ipc.c b/src/backend/storage/ipc/ipc.c
index 6bc0b06..bfc7764 100644
--- a/src/backend/storage/ipc/ipc.c
+++ b/src/backend/storage/ipc/ipc.c
@@ -291,10 +291,12 @@ atexit_callback(void)
 void
 on_proc_exit(pg_on_exit_callback function, Datum arg)
 {
-	if (on_proc_exit_index >= MAX_ON_EXITS)
-		ereport(FATAL,
-				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-				 errmsg_internal("out of on_proc_exit slots")));
+	int 	i;
+	
+	/* check no duplicates and there are rooms */
+	for (i = 0; i < on_proc_exit_index; i++)
+		Assert (on_proc_exit_list[i] != function);
+	Assert (on_proc_exit_index < MAX_ON_EXITS);
 
 	on_proc_exit_list[on_proc_exit_index].function = function;
 	on_proc_exit_list[on_proc_exit_index].arg = arg;
@@ -319,10 +321,12 @@ on_proc_exit(pg_on_exit_callback function, Datum arg)
 void
 before_shmem_exit(pg_on_exit_callback function, Datum arg)
 {
-	if (before_shmem_exit_index >= MAX_ON_EXITS)
-		ereport(FATAL,
-				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-				 errmsg_internal("out of before_shmem_exit slots")));
+	int		i;
+	
+	/* check no duplicated callbacks and there are rooms */
+	for (i = 0; i < before_shmem_exit_index; i++)
+		Assert (before_shmem_exit_list[i] != function);
+	Assert (before_shmem_exit_index < MAX_ON_EXITS);
 
 	before_shmem_exit_list[before_shmem_exit_index].function = function;
 	before_shmem_exit_list[before_shmem_exit_index].arg = arg;
@@ -347,10 +351,12 @@ before_shmem_exit(pg_on_exit_callback function, Datum arg)
 void
 on_shmem_exit(pg_on_exit_callback function, Datum arg)
 {
-	if (on_shmem_exit_index >= MAX_ON_EXITS)
-		ereport(FATAL,
-				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
-				 errmsg_internal("out of on_shmem_exit slots")));
+	int 	i;
+	
+	/* check no duplicates and there are rooms */
+	for (i = 0; i < on_shmem_exit_index; i++)
+		Assert (on_shmem_exit_list[i] != function);
+	Assert (on_shmem_exit_index < MAX_ON_EXITS);
 
 	on_shmem_exit_list[on_shmem_exit_index].function = function;
 	on_shmem_exit_list[on_shmem_exit_index].arg = arg;
#2Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Qingqing Zhou (#1)
Re: Assert there is no duplicated exit callbacks

湏〠⼴㔱㈯㄰‵㜰㔺′䵐‬楑杮楱杮娠潨⁵牷瑯㩥㸊䄠瑴捡敨⁤獩愠瀠瑡档琠獡敳瑲琠敨敲椠⁳潮搠灵楬慣整⁤硥瑩挠污扬捡獫ਮ䤊搠湯琧爠浥浥敢⁲桴瑡攠敶⁲敢湥愠瀠潲汢浥ਮ㸊䄠潬杮琠敨眠祡‬⁉潤湷牧摡⁥桴⁥畲瑮浩⁥攢潮杵⁨潲浯•档捥潴愠੮‾獡敳瑲潩㩮琠敨挠污扬捡獫愠敲爠来獩整敲⁤湩瀠敲瑴⁹楦數⁤湩瑩慩楬慺楴湯㸊挠摯⁥慰桴‬桴獵愠獳牥楴湯椠⁳潧摯攠潮杵⁨潴瀠敲敶瑮漠敶汲潯桴牥⹥䤠੦‾敷搠湯琧愠牧敥眠瑩⁨桴獩‬桴湥琠敨搠灵楬慣楴湯挠敨正猠慨汬愠獬畢灭㸊琠畲瑮浩⁥档捥獫ਮ䤊搠湯琧琠楨歮愠祮漠⁦桴獩椠⁳敮摥摥ਮⴊ䠠楥歫੩ਊⴊ‭匊湥⁴楶⁡杰煳⵬慨正牥⁳慭汩湩⁧楬瑳⠠杰煳⵬慨正牥䁳潰瑳牧獥汱漮杲਩潔洠歡⁥档湡敧⁳潴礠畯⁲畳獢牣灩楴湯਺瑨灴⼺眯睷瀮獯杴敲煳⹬牯⽧慭汩牰晥瀯獧汱栭捡敫獲ਊ

#3Qingqing Zhou
zhouqq.postgres@gmail.com
In reply to: Heikki Linnakangas (#2)
Re: Assert there is no duplicated exit callbacks

Hmm, the email text seems corrupted?

Regards,
Qingqing

On Wed, Apr 15, 2015 at 10:03 AM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:

湏〠⼴㔱㈯㄰‵㜰㔺′䵐‬楑杮楱杮娠潨⁵牷瑯㩥਍‾瑁慴档摥椠⁳⁡慰捴⁨潴愠獳牥⁴

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#4David Fetter
david@fetter.org
In reply to: Qingqing Zhou (#3)
Re: Assert there is no duplicated exit callbacks

On Wed, Apr 15, 2015 at 11:14:43AM -0700, Qingqing Zhou wrote:

Hmm, the email text seems corrupted?

I think it was an encoding issue. Here's what I got when I piped it
through less.

Cheers,
David.

On 04/15/2015 07:52 PM, Qingqing Zhou wrote:

Attached is a patch to assert there is no duplicated exit callbacks.

I don't remember that ever been a problem.

Along the way, I downgrade the runtime "enough room" check to an
assertion: the callbacks are registered in pretty fixed initialization
code path, thus assertion is good enough to prevent overlook there. If
we don't agree with this, then the duplication check shall also bump
to runtime checks.

I don't think any of this is needed.

- Heikki
--
David Fetter <david@fetter.org> http://fetter.org/
Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter
Skype: davidfetter XMPP: david.fetter@gmail.com

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers