ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

Started by Alexander Pyhalovabout 2 years ago12 messageshackers
Jump to latest
#1Alexander Pyhalov
a.pyhalov@postgrespro.ru

Hi.

Recent commit 555276f8594087ba15e0d58e38cd2186b9f39f6d introduced final
cleanup of node->as_eventset in ExecAppendAsyncEventWait().
Unfortunately, now this function can return in the middle of TRY/FINALLY
block, without restoring PG_exception_stack.

We found this while working on our FDW. Unfortunately, I couldn't
reproduce the issue with postgres_fdw, but it seems it is also affected.

The following patch heals the issue.

-- l
Best regards,
Alexander Pyhalov,
Postgres Professional

Attachments:

0001-Avoid-corrupting-PG_exception_stack-in-ExecAppendAsy.patchtext/x-diff; name=0001-Avoid-corrupting-PG_exception_stack-in-ExecAppendAsy.patchDownload+14-14
#2Michael Paquier
michael@paquier.xyz
In reply to: Alexander Pyhalov (#1)
Re: ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

On Fri, Feb 23, 2024 at 01:21:14PM +0300, Alexander Pyhalov wrote:

Recent commit 555276f8594087ba15e0d58e38cd2186b9f39f6d introduced final
cleanup of node->as_eventset in ExecAppendAsyncEventWait().
Unfortunately, now this function can return in the middle of TRY/FINALLY
block, without restoring PG_exception_stack.

We found this while working on our FDW. Unfortunately, I couldn't reproduce
the issue with postgres_fdw, but it seems it is also affected.

Ugh, yes, you are obviously right that the early return is wrong.
I'll look into fixing that where appropriate. Thanks for the report,
Alexander!
--
Michael

#3Etsuro Fujita
fujita.etsuro@lab.ntt.co.jp
In reply to: Michael Paquier (#2)
Re: ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

Hi,

On Sat, Feb 24, 2024 at 10:06 AM Michael Paquier <michael@paquier.xyz> wrote:

On Fri, Feb 23, 2024 at 01:21:14PM +0300, Alexander Pyhalov wrote:

Recent commit 555276f8594087ba15e0d58e38cd2186b9f39f6d introduced final
cleanup of node->as_eventset in ExecAppendAsyncEventWait().
Unfortunately, now this function can return in the middle of TRY/FINALLY
block, without restoring PG_exception_stack.

We found this while working on our FDW. Unfortunately, I couldn't reproduce
the issue with postgres_fdw, but it seems it is also affected.

I think this would happen when FDWs configure no events; IIRC I think
while the core allows them to do so, postgres_fdw does not do so, so
this would never happen with it. Anyway, thanks for the report and
patch, Alexander!

Ugh, yes, you are obviously right that the early return is wrong.
I'll look into fixing that where appropriate.

Thanks for taking care of this, Michael-san! This would result
originally from my fault, so If you don't mind, could you let me do
that?

Best regards,
Etsuro Fujita

#4Michael Paquier
michael@paquier.xyz
In reply to: Etsuro Fujita (#3)
Re: ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

On Sun, Feb 25, 2024 at 06:34:30PM +0900, Etsuro Fujita wrote:

I think this would happen when FDWs configure no events; IIRC I think
while the core allows them to do so, postgres_fdw does not do so, so
this would never happen with it. Anyway, thanks for the report and
patch, Alexander!

I don't see how that's directly your fault as this is a thinko in the
set of commits 481d7d1c01, 555276f859 and 501cfd07da that have hit
14~16, ignoring entirely the TRY/CATCH block.

Anyway, if you want to address it yourself, feel free to go ahead,
thanks! I would have done it but I've been busy with life stuff for
the last couple of days.
--
Michael

#5Etsuro Fujita
fujita.etsuro@lab.ntt.co.jp
In reply to: Michael Paquier (#4)
Re: ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

On Mon, Feb 26, 2024 at 8:37 AM Michael Paquier <michael@paquier.xyz> wrote:

I don't see how that's directly your fault as this is a thinko in the
set of commits 481d7d1c01, 555276f859 and 501cfd07da that have hit
14~16, ignoring entirely the TRY/CATCH block.

The set of commits is actually a fix for resource leaks in my commit 27e1f1456.

Anyway, if you want to address it yourself, feel free to go ahead,
thanks! I would have done it but I've been busy with life stuff for
the last couple of days.

Will do. (I was thinking you would get busy from now on.)

Best regards,
Etsuro Fujita

#6Michael Paquier
michael@paquier.xyz
In reply to: Etsuro Fujita (#5)
Re: ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

On Mon, Feb 26, 2024 at 04:29:44PM +0900, Etsuro Fujita wrote:

Will do. (I was thinking you would get busy from now on.)

Fujita-san, have you been able to look at this thread?
--
Michael

#7Etsuro Fujita
fujita.etsuro@lab.ntt.co.jp
In reply to: Michael Paquier (#6)
Re: ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

Hi Michael-san,

On Mon, Mar 11, 2024 at 8:12 AM Michael Paquier <michael@paquier.xyz> wrote:

On Mon, Feb 26, 2024 at 04:29:44PM +0900, Etsuro Fujita wrote:

Will do. (I was thinking you would get busy from now on.)

Fujita-san, have you been able to look at this thread?

Yeah, I took a look; the patch looks good to me, but I am thiking to
update some comments in a related function in postgres_fdw.c. I will
have time to work on this later this week, so I would like to propose
an updated patch then.

Thanks for taking care of this!

Best regards,
Etsuro Fujita

#8Etsuro Fujita
fujita.etsuro@lab.ntt.co.jp
In reply to: Etsuro Fujita (#3)
Re: ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

On Sun, Feb 25, 2024 at 6:34 PM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:

On Fri, Feb 23, 2024 at 01:21:14PM +0300, Alexander Pyhalov wrote:

Recent commit 555276f8594087ba15e0d58e38cd2186b9f39f6d introduced final
cleanup of node->as_eventset in ExecAppendAsyncEventWait().
Unfortunately, now this function can return in the middle of TRY/FINALLY
block, without restoring PG_exception_stack.

We found this while working on our FDW. Unfortunately, I couldn't reproduce
the issue with postgres_fdw, but it seems it is also affected.

I think this would happen when FDWs configure no events; IIRC I think
while the core allows them to do so, postgres_fdw does not do so, so
this would never happen with it.

I was wrong; as you pointed out, this would affect postgres_fdw as
well. See commit 1ec7fca85, which is my commit, but I forgot it
completely. :-(

As I said before, the patch looks good to me. I tweaked comments in
ExecAppendAsyncEventWait() a bit. Attached is an updated patch. In
the patch I also fixed a confusing comment in a related function in
postgres_fdw.c about handling of the in-process request that might be
useless to process.

Sorry, it took more time than expected to get back to this thread.

Best regards,
Etsuro Fujita

Attachments:

0001-Avoid-corrupting-PG_exception_stack-in-ExecAppendAsy-v2.patchapplication/octet-stream; name=0001-Avoid-corrupting-PG_exception_stack-in-ExecAppendAsy-v2.patchDownload+25-24
#9Alexander Pyhalov
a.pyhalov@postgrespro.ru
In reply to: Etsuro Fujita (#8)
Re: ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

Etsuro Fujita писал(а) 2024-03-21 13:59:

On Sun, Feb 25, 2024 at 6:34 PM Etsuro Fujita <etsuro.fujita@gmail.com>
wrote:

On Fri, Feb 23, 2024 at 01:21:14PM +0300, Alexander Pyhalov wrote:

Recent commit 555276f8594087ba15e0d58e38cd2186b9f39f6d introduced final
cleanup of node->as_eventset in ExecAppendAsyncEventWait().
Unfortunately, now this function can return in the middle of TRY/FINALLY
block, without restoring PG_exception_stack.

We found this while working on our FDW. Unfortunately, I couldn't reproduce
the issue with postgres_fdw, but it seems it is also affected.

I think this would happen when FDWs configure no events; IIRC I think
while the core allows them to do so, postgres_fdw does not do so, so
this would never happen with it.

I was wrong; as you pointed out, this would affect postgres_fdw as
well. See commit 1ec7fca85, which is my commit, but I forgot it
completely. :-(

As I said before, the patch looks good to me. I tweaked comments in
ExecAppendAsyncEventWait() a bit. Attached is an updated patch. In
the patch I also fixed a confusing comment in a related function in
postgres_fdw.c about handling of the in-process request that might be
useless to process.

Sorry, it took more time than expected to get back to this thread.

Hi. The updated patch still looks good to me.
--
Best regards,
Alexander Pyhalov,
Postgres Professional

#10Etsuro Fujita
fujita.etsuro@lab.ntt.co.jp
In reply to: Alexander Pyhalov (#9)
Re: ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

Hi Alexander,

On Fri, Mar 22, 2024 at 7:23 PM Alexander Pyhalov
<a.pyhalov@postgrespro.ru> wrote:

The updated patch still looks good to me.

Great! I am planning to apply the patch to the back branches next week.

Thanks for the review!

Best regards,
Etsuro Fujita

#11Etsuro Fujita
fujita.etsuro@lab.ntt.co.jp
In reply to: Etsuro Fujita (#10)
Re: ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

On Fri, Mar 22, 2024 at 9:09 PM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:

On Fri, Mar 22, 2024 at 7:23 PM Alexander Pyhalov
<a.pyhalov@postgrespro.ru> wrote:

The updated patch still looks good to me.

I am planning to apply the patch to the back branches next week.

Pushed. Sorry for the delay.

Best regards,
Etsuro Fujita

#12Michael Paquier
michael@paquier.xyz
In reply to: Etsuro Fujita (#11)
Re: ExecAppendAsyncEventWait() in REL_14_STABLE can corrupt PG_exception_stack

On Thu, Apr 04, 2024 at 06:08:40PM +0900, Etsuro Fujita wrote:

Pushed. Sorry for the delay.

Thanks!
--
Michael