pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedBug in WaitForBackgroundWorkerShutdown() [REL9_5_STABLE]
4+ messages / 3 participants
[nested] [flat]
* Bug in WaitForBackgroundWorkerShutdown() [REL9_5_STABLE]
@ 2016-08-04 11:44 Dmitry Ivanov <d.ivanov@postgrespro.ru>
0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Ivanov @ 2016-08-04 11:44 UTC (permalink / raw)
To: pgsql-hackers
Recently I've encountered a strange crash while calling elog(ERROR, "...")
after the WaitForBackgroundWorkerShutdown() function. It turns out that
_returns_ inside the PG_TRY()..PG_CATCH() block are *highly* undesirable,
since they leave PG_exception_stack pointing to a local struct in a dead
frame, which is an obvious UB. I've attached a patch which fixes this behavior
in the aforementioned function, but there might be more errors like that
elsewhere.
--
Dmitry Ivanov
Postgres Professional: http://www.postgrespro.com
Russian Postgres Company
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Attachments:
fix_WaitForBackgroundWorkerShutdown_v1.patch (text/x-patch, 649B) download
diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c
index 76619e9..5ecb55a 100644
--- a/src/backend/postmaster/bgworker.c
+++ b/src/backend/postmaster/bgworker.c
@@ -1025,13 +1025,14 @@ WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
status = GetBackgroundWorkerPid(handle, &pid);
if (status == BGWH_STOPPED)
- return status;
+ break;
rc = WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_POSTMASTER_DEATH, 0);
if (rc & WL_POSTMASTER_DEATH)
- return BGWH_POSTMASTER_DIED;
+ status = BGWH_POSTMASTER_DIED;
+ break;
ResetLatch(&MyProc->procLatch);
}
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Bug in WaitForBackgroundWorkerShutdown() [REL9_5_STABLE]
@ 2016-08-04 11:47 Dmitry Ivanov <d.ivanov@postgrespro.ru>
parent: Dmitry Ivanov <d.ivanov@postgrespro.ru>
0 siblings, 1 reply; 4+ messages in thread
From: Dmitry Ivanov @ 2016-08-04 11:47 UTC (permalink / raw)
To: pgsql-hackers
> Recently I've encountered a strange crash while calling elog(ERROR, "...")
> after the WaitForBackgroundWorkerShutdown() function. It turns out that
> _returns_ inside the PG_TRY()..PG_CATCH() block are *highly* undesirable,
> since they leave PG_exception_stack pointing to a local struct in a dead
> frame, which is an obvious UB. I've attached a patch which fixes this
> behavior in the aforementioned function, but there might be more errors
> like that elsewhere.
Forgot some curly braces, my bad. v2 attached.
--
Dmitry Ivanov
Postgres Professional: http://www.postgrespro.com
Russian Postgres Company
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Attachments:
fix_WaitForBackgroundWorkerShutdown_v2.patch (text/x-patch, 661B) download
diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c
index 76619e9..fd55262 100644
--- a/src/backend/postmaster/bgworker.c
+++ b/src/backend/postmaster/bgworker.c
@@ -1025,13 +1025,16 @@ WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
status = GetBackgroundWorkerPid(handle, &pid);
if (status == BGWH_STOPPED)
- return status;
+ break;
rc = WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_POSTMASTER_DEATH, 0);
if (rc & WL_POSTMASTER_DEATH)
- return BGWH_POSTMASTER_DIED;
+ {
+ status = BGWH_POSTMASTER_DIED;
+ break;
+ }
ResetLatch(&MyProc->procLatch);
}
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Bug in WaitForBackgroundWorkerShutdown() [REL9_5_STABLE]
@ 2016-08-04 12:08 Yury Zhuravlev <u.zhuravlev@postgrespro.ru>
parent: Dmitry Ivanov <d.ivanov@postgrespro.ru>
0 siblings, 1 reply; 4+ messages in thread
From: Yury Zhuravlev @ 2016-08-04 12:08 UTC (permalink / raw)
To: pgsql-hackers
Dmitry Ivanov wrote:
>> Recently I've encountered a strange crash while calling elog(ERROR, "...")
>> after the WaitForBackgroundWorkerShutdown() function. It turns out that
>> _returns_ inside the PG_TRY()..PG_CATCH() block are *highly* undesirable,
>> since they leave PG_exception_stack pointing to a local struct in a dead
>> frame, which is an obvious UB. I've attached a patch which fixes this
>> behavior in the aforementioned function, but there might be more errors
>> like that elsewhere.
>
> Forgot some curly braces, my bad. v2 attached.
>
Good catch. But in 9.6 it has been fixed by
db0f6cad4884bd4c835156d3a720d9a79dbd63a9 commit.
--
Yury Zhuravlev
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Bug in WaitForBackgroundWorkerShutdown() [REL9_5_STABLE]
@ 2016-08-04 20:19 Tom Lane <tgl@sss.pgh.pa.us>
parent: Yury Zhuravlev <u.zhuravlev@postgrespro.ru>
0 siblings, 0 replies; 4+ messages in thread
From: Tom Lane @ 2016-08-04 20:19 UTC (permalink / raw)
To: Dmitry Ivanov <d.ivanov@postgrespro.ru>; +Cc: pgsql-hackers; Yury Zhuravlev <u.zhuravlev@postgrespro.ru>
Yury Zhuravlev <u.zhuravlev@postgrespro.ru> writes:
> Dmitry Ivanov wrote:
>>> Recently I've encountered a strange crash while calling elog(ERROR, "...")
>>> after the WaitForBackgroundWorkerShutdown() function. It turns out that
>>> _returns_ inside the PG_TRY()..PG_CATCH() block are *highly* undesirable,
>>> since they leave PG_exception_stack pointing to a local struct in a dead
>>> frame, which is an obvious UB. I've attached a patch which fixes this
>>> behavior in the aforementioned function, but there might be more errors
>>> like that elsewhere.
> Good catch. But in 9.6 it has been fixed by
> db0f6cad4884bd4c835156d3a720d9a79dbd63a9 commit.
Only accidentally, I'd say. I chose to push this into HEAD as well,
so that WaitForBackgroundWorkerShutdown would look more like
WaitForBackgroundWorkerStartup, and would not have a risk of future
breakage if someone adds code after the for-loop.
I was pretty worried about the "more errors like that" point, because
we recently fixed another similar mistake in plpython, cf 1d2fe56e4.
However, a search using the attached quick-hack script to look for
"return" etc between PG_TRY and PG_CATCH did not find any other
instances.
It'd be nice to have some automatic way of catching future mistakes
like this, but I'm not sure about a reliable way to do that.
One idea is to add an Assert to PG_CATCH:
#define PG_TRY() \
do { \
sigjmp_buf *save_exception_stack = PG_exception_stack; \
ErrorContextCallback *save_context_stack = error_context_stack; \
sigjmp_buf local_sigjmp_buf; \
if (sigsetjmp(local_sigjmp_buf, 0) == 0) \
{ \
PG_exception_stack = &local_sigjmp_buf
#define PG_CATCH() \
+ Assert(PG_exception_stack == &local_sigjmp_buf); \
} \
else \
{ \
PG_exception_stack = save_exception_stack; \
error_context_stack = save_context_stack
but there are a couple of disadvantages to this. One is that you don't
find out about a problem unless the bad code path is actually exercised.
I'm afraid that the most tempting places to do something like this would
be low-probability error cases, and thus that the Assert would do little
to catch them. (Case in point: the postmaster-death path fixed in this
patch would surely never have been caught by testing.) The other and
really worse problem is that when the Assert does fire, it's nowhere near
the scene of the crime, so while it may tell you there's a problem it
does not give much help in locating the bug.
Anyone have a better idea?
regards, tom lane
--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
Attachments:
find_bad_pg_try (text/x-shellscript, 330B) download
#!/bin/sh
# search DIR for PG_TRY blocks containing WORD
# return, break, continue, goto are good things to look for
dir="$1"
word="$2"
for f in `find "$dir" -type f -name '*.c'`
do
sed -n -e '/PG_TRY()/,/PG_CATCH()/ { /'"$word"'/ =; /'"$word"'/ p }' $f | \
sed -e '/^[0-9]\+$/ { s|^[0-9]\+$|'"$f"': &: |; N }'
done
exit 0
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2016-08-04 20:19 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-08-04 11:44 Bug in WaitForBackgroundWorkerShutdown() [REL9_5_STABLE] Dmitry Ivanov <d.ivanov@postgrespro.ru>
2016-08-04 11:47 ` Dmitry Ivanov <d.ivanov@postgrespro.ru>
2016-08-04 12:08 ` Yury Zhuravlev <u.zhuravlev@postgrespro.ru>
2016-08-04 20:19 ` Tom Lane <tgl@sss.pgh.pa.us>
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