GetNamedLWLockTranche crashes on Windows in normal backend

Started by Sami Imseihover 1 year ago33 messageshackers
Jump to latest
#1Sami Imseih
samimseih@gmail.com

Hi,

While working on [0]/messages/by-id/CAA5RZ0vvED3naph8My8Szv6DL4AxOVK3eTPS0qXsaKi=bVdW2A@mail.gmail.com, I observed that $SUBJECT. I encountered this issue
while building test cases for [0]/messages/by-id/CAA5RZ0vvED3naph8My8Szv6DL4AxOVK3eTPS0qXsaKi=bVdW2A@mail.gmail.com, and in which GetNamedLWLockTranche is
called outside of startup.

This crash, reproducible on HEAD, occurs because NamedLWLockTrancheRequestArray
( a local array populated during shmem_request ) is not copied over in
exec:ed backends.
This is reproducible on all EXEC_BACKEND environments.
On Linux, this is not an issue because of the magic of fork.

Since this data is not in shared memory, it will need much extra handling
in launch_backend.c, if at all safe, to get it copied. I am not too
familiar with
EXEC_BACKEND, so I could be wrong.
NamedLWLockTrancheRequests, which tracks the length of
NamedLWLockTrancheRequestArray is copied successfully.

Now, I wonder if the correct solution is just to allow GetNamedLWLockTranche
to be called ONLY when !IsUnderPostmaster, and error out if that's not
the case.
The docs [1]https://www.postgresql.org/docs/current/xfunc-c.html#XFUNC-SHARED-ADDIN-AT-STARTUP for the function mention it should be used during startup, and I
can't envision a reason one would want to use it outside.

We can also create a new global bool for shmem_startup progress, similar to
process_shmem_requests_in_progress, and ensure that this function is only
called at that time.

I repro'd this on a Windows machine, but one can also enable EXEC_BACKEND
in pg_config_manual.h and call GetNamedLWLockTranche by a normal backend.

I did this by injecting
```
GetNamedLWLockTranche("pg_stat_statements");
```
inside pgss_ExecutorEnd

[0]: /messages/by-id/CAA5RZ0vvED3naph8My8Szv6DL4AxOVK3eTPS0qXsaKi=bVdW2A@mail.gmail.com
[1]: https://www.postgresql.org/docs/current/xfunc-c.html#XFUNC-SHARED-ADDIN-AT-STARTUP

--
Sami Imseih
Amazon Web Services (AWS)

#2Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#1)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Fri, Aug 22, 2025 at 02:21:55PM -0500, Sami Imseih wrote:

While working on [0], I observed that $SUBJECT. I encountered this issue
while building test cases for [0], and in which GetNamedLWLockTranche is
called outside of startup.

[...]

I repro'd this on a Windows machine, but one can also enable EXEC_BACKEND
in pg_config_manual.h and call GetNamedLWLockTranche by a normal backend.

I did this by injecting
```
GetNamedLWLockTranche("pg_stat_statements");
```
inside pgss_ExecutorEnd

If this fails, why doesn't the call in pgss_shmem_startup() also fail? Was
pg_stat_statements not loaded via shared_preload_libraries? And is the
failure a segfault or a "requested tranche is not registered" error?

--
nathan

#3Sami Imseih
samimseih@gmail.com
In reply to: Nathan Bossart (#2)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Fri, Aug 22, 2025 at 02:21:55PM -0500, Sami Imseih wrote:

While working on [0], I observed that $SUBJECT. I encountered this issue
while building test cases for [0], and in which GetNamedLWLockTranche is
called outside of startup.

[...]

I repro'd this on a Windows machine, but one can also enable EXEC_BACKEND
in pg_config_manual.h and call GetNamedLWLockTranche by a normal backend.

I did this by injecting
```
GetNamedLWLockTranche("pg_stat_statements");
```
inside pgss_ExecutorEnd

If this fails, why doesn't the call in pgss_shmem_startup() also fail? Was
pg_stat_statements not loaded via shared_preload_libraries?

because the array is valid in postmaster, but it's not for a normal backend,
since it's not getting copied.

Yes, pg_stat_statements was loaded via shared_preload_libraries. Nothing
was done out of the ordinary.

And is the
failure a segfault or a "requested tranche is not registered" error?

It's a segfault.

--
Sami

#4Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#3)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Mon, Aug 25, 2025 at 12:58:08PM -0500, Sami Imseih wrote:

If this fails, why doesn't the call in pgss_shmem_startup() also fail? Was
pg_stat_statements not loaded via shared_preload_libraries?

because the array is valid in postmaster, but it's not for a normal backend,
since it's not getting copied.

Yes, pg_stat_statements was loaded via shared_preload_libraries. Nothing
was done out of the ordinary.

And is the
failure a segfault or a "requested tranche is not registered" error?

It's a segfault.

Oh, I see what's happening. We are never calling GetNamedLWLockTranche()
in backends because ShmemInitStruct() always sets *found.

I'd argue that calling GetNamedLWLockTranche() outside of an "if (!found)"
block in a shmem_startup_hook doesn't follow convention, but the docs have
this note:

shmem_startup_hook provides a convenient place for the initialization
code, but it is not strictly required that all such code be placed in
this hook.

Like the inaccurate sentence immediately following this one [0]/messages/by-id/aJ9QIF3g2QQko9wq@nathan, it was
added by commit 964152c. IMHO we should adjust the documentation to match
reality.

[0]: /messages/by-id/aJ9QIF3g2QQko9wq@nathan

--
nathan

#5Sami Imseih
samimseih@gmail.com
In reply to: Nathan Bossart (#4)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Mon, Aug 25, 2025 at 12:58:08PM -0500, Sami Imseih wrote:

If this fails, why doesn't the call in pgss_shmem_startup() also fail? Was
pg_stat_statements not loaded via shared_preload_libraries?

because the array is valid in postmaster, but it's not for a normal backend,
since it's not getting copied.

Yes, pg_stat_statements was loaded via shared_preload_libraries. Nothing
was done out of the ordinary.

And is the
failure a segfault or a "requested tranche is not registered" error?

It's a segfault.

Oh, I see what's happening. We are never calling GetNamedLWLockTranche()
in backends because ShmemInitStruct() always sets *found.

I'd argue that calling GetNamedLWLockTranche() outside of an "if (!found)"
block in a shmem_startup_hook doesn't follow convention, but the docs have
this note:

shmem_startup_hook provides a convenient place for the initialization
code, but it is not strictly required that all such code be placed in
this hook.

Like the inaccurate sentence immediately following this one [0], it was
added by commit 964152c. IMHO we should adjust the documentation to match
reality.

[0] /messages/by-id/aJ9QIF3g2QQko9wq@nathan

Why not ERROR out completely if we are calling this function outside
of postmaster
startup? I don't think we should be satisfied with just a
documentation change, since this
leads to a segfault. We can do something like below at the top of
GetNamedLWLockTranche

```
if (IsUnderPostmaster)
elog(ERROR, "GetNamedLWLockTranche can only be called during
postmaster startup");
```

Another approach is to just change GetNamedLWLockTranche to use
NamedLWLockTrancheArray since that is already copied in EXEC_BACKEND, and allow
GetNamedLWLockTranche to continue to be used outside of startup. In
this case, we
will need to add num_lwlocks field to NamedLWLockTrancheArray. This
might be better
to backpatch, since we will not be changing user facing behavior.

Attached is a repro patch. You will need to set
shared_preload_libraries = 'pg_stat_statements'
as well.

--
Sami

Attachments:

v1-0001-repro.patchapplication/octet-stream; name=v1-0001-repro.patchDownload+3-4
#6Sami Imseih
samimseih@gmail.com
In reply to: Sami Imseih (#5)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

Attached is a repro patch. You will need to set
shared_preload_libraries = 'pg_stat_statements'
as well.

I forgot to mention. Once patched, you can run a simple
select statement from a new connection to force the crash.
This is because GetNamedLWLockTranche will be called
at pgss_ExecutorEnd.

--
Sami

#7Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#5)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Mon, Aug 25, 2025 at 01:44:22PM -0500, Sami Imseih wrote:

Another approach is to just change GetNamedLWLockTranche to use
NamedLWLockTrancheArray since that is already copied in EXEC_BACKEND, and
allow GetNamedLWLockTranche to continue to be used outside of startup. In
this case, we will need to add num_lwlocks field to
NamedLWLockTrancheArray. This might be better to backpatch, since we will
not be changing user facing behavior.

That seems like a reasonable thing to try. It looks like
NamedLWLockTrancheRequests is copied, too. Could we used that instead of
adding a new variable?

My guess is that this has been broken since this code was introduced in
commit c1772ad from 2016/v9.6, and AFAIK you are the first to report it, so
I don't feel a tremendous amount of urgency to fix it on the back-branches.

--
nathan

#8Sami Imseih
samimseih@gmail.com
In reply to: Nathan Bossart (#7)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

Another approach is to just change GetNamedLWLockTranche to use
NamedLWLockTrancheArray since that is already copied in EXEC_BACKEND, and
allow GetNamedLWLockTranche to continue to be used outside of startup. In
this case, we will need to add num_lwlocks field to
NamedLWLockTrancheArray. This might be better to backpatch, since we will
not be changing user facing behavior.

That seems like a reasonable thing to try. It looks like
NamedLWLockTrancheRequests is copied, too. Could we used that instead of
adding a new variable?

We would not need a new variable. Currently it's assumed that
NamedLWLockTrancheRequests will be the size of both
NamedLWLockTrancheRequests and NamedLWLockTrancheArray.

```
/*
* NamedLWLockTrancheRequests is both the valid length of the request array,
* and the length of the shared-memory NamedLWLockTrancheArray later on.
* This variable and NamedLWLockTrancheArray are non-static so that
* postmaster.c can copy them to child processes in EXEC_BACKEND builds.
*/
int NamedLWLockTrancheRequests = 0;
```

My guess is that this has been broken since this code was introduced in
commit c1772ad from 2016/v9.6, and AFAIK you are the first to report it, so
I don't feel a tremendous amount of urgency to fix it on the back-branches.

Yeah, I was doing something for testing that is not likely to be done in
the real-world. Also, considering this is an EXEC_BACKEND case only,
it makes it even less likely.

--
Sami

#9Sami Imseih
samimseih@gmail.com
In reply to: Sami Imseih (#8)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

Here it is.

I was hoping we can even get rid of NamedLWLockTrancheRequests
altogether, but that's not going to be possible because
RequestNamedLWLockTranche happens earlier than
CreateLWLocks, so NamedLWLockTrancheRequests is essentially
tracking the requested lwlocks until we get a chance to create and
initialize the LWLocks and store them in the shared memory,
NamedLWLockTrancheArray.

--
Sami

Attachments:

0001-Fix-EXEC_BACKEND-segfault-on-NamedLWLockTrancheReque.patchapplication/octet-stream; name=0001-Fix-EXEC_BACKEND-segfault-on-NamedLWLockTrancheReque.patchDownload+4-3
#10Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#9)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Mon, Aug 25, 2025 at 04:28:09PM -0500, Sami Imseih wrote:

Here it is.

Looks reasonable to me. I'll leave this one around for a week or two to
give others a chance to comment.

--
nathan

#11Nathan Bossart
nathandbossart@gmail.com
In reply to: Nathan Bossart (#10)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

Well, commit 38b602b certainly doesn't do us any favors here since it
removed NamedLWLockTrancheArray. Given the lack of reports from the field,
I suspect the best path forward is to add an ERROR for unsafe accesses and
to fix the documentation, as discussed upthread.

--
nathan

#12Sami Imseih
samimseih@gmail.com
In reply to: Nathan Bossart (#11)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

commit 38b602b certainly doesn't do us any favors here since it

removed NamedLWLockTrancheArray. Given the lack of reports from the field,
I suspect the best path forward is to add an ERROR for unsafe accesses and
to fix the documentation, as discussed upthread.

I suggest we set an in progres flag for shmem_startup_hook, as we do for
shmem_request_hook, and error out this function if called outside of this
hook. The error will be something like this:

```
ERROR: cannot return the address of LWLocks outside of shmem_startup_hook
```

We can also update the docs.

If you agree with the above, I'll get the patch ready?

--
Sami Imseih
Amazon Web Services (AWS)

#13Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#12)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Thu, Sep 04, 2025 at 01:23:26PM -0500, Sami Imseih wrote:

I suggest we set an in progres flag for shmem_startup_hook, as we do for
shmem_request_hook, and error out this function if called outside of this
hook. The error will be something like this:

```
ERROR: cannot return the address of LWLocks outside of shmem_startup_hook
```

We can also update the docs.

If you agree with the above, I'll get the patch ready?

Yeah, I think modeling this after commit 4f2400c is a reasonable thing to
explore.

--
nathan

#14Sami Imseih
samimseih@gmail.com
In reply to: Nathan Bossart (#13)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

Yeah, I think modeling this after commit 4f2400c is a reasonable thing to
explore.

Here it is as described above.

--
Sami

Attachments:

v2-0001-Prevent-crash-when-retreiving-LWLock-tranches.patchapplication/octet-stream; name=v2-0001-Prevent-crash-when-retreiving-LWLock-tranches.patchDownload+25-6
#15Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#14)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Thu, Sep 04, 2025 at 04:12:09PM -0500, Sami Imseih wrote:

Yeah, I think modeling this after commit 4f2400c is a reasonable thing to
explore.

Here it is as described above.

Thanks. This looks like the right idea to me, but let's give some time for
others to comment.

--
nathan

#16Nathan Bossart
nathandbossart@gmail.com
In reply to: Nathan Bossart (#15)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Thu, Sep 04, 2025 at 08:35:13PM -0500, Nathan Bossart wrote:

On Thu, Sep 04, 2025 at 04:12:09PM -0500, Sami Imseih wrote:

Yeah, I think modeling this after commit 4f2400c is a reasonable thing to
explore.

Here it is as described above.

Thanks. This looks like the right idea to me, but let's give some time for
others to comment.

I've started preparing this for commit, and I realized that restricting
GetNamedLWLockTranche() to shmem_startup_hook is not sufficient.
EXEC_BACKEND builds will run this hook in every backend, so unless it's
guarded behind some sort of "if (!found)" condition (i.e., only run in the
postmaster), it'll still crash. I think we just need to add some extra
notes to the docs and check for !IsUnderPostmaster, as discussed upthread.

--
nathan

#17Nathan Bossart
nathandbossart@gmail.com
In reply to: Nathan Bossart (#16)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Mon, Sep 08, 2025 at 11:55:14AM -0500, Nathan Bossart wrote:

I've started preparing this for commit, and I realized that restricting
GetNamedLWLockTranche() to shmem_startup_hook is not sufficient.
EXEC_BACKEND builds will run this hook in every backend, so unless it's
guarded behind some sort of "if (!found)" condition (i.e., only run in the
postmaster), it'll still crash. I think we just need to add some extra
notes to the docs and check for !IsUnderPostmaster, as discussed upthread.

Or... what if we just moved the request array to shared memory?

--
nathan

Attachments:

v3-0001-Move-named-LWLock-tranche-request-array-to-shared.patchtext/plain; charset=us-asciiDownload+33-6
#18Sami Imseih
samimseih@gmail.com
In reply to: Nathan Bossart (#16)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Thu, Sep 04, 2025 at 08:35:13PM -0500, Nathan Bossart wrote:

On Thu, Sep 04, 2025 at 04:12:09PM -0500, Sami Imseih wrote:

Yeah, I think modeling this after commit 4f2400c is a reasonable thing to
explore.

Here it is as described above.

Thanks. This looks like the right idea to me, but let's give some time for
others to comment.

I've started preparing this for commit, and I realized that restricting
GetNamedLWLockTranche() to shmem_startup_hook is not sufficient.
EXEC_BACKEND builds will run this hook in every backend, so unless it's
guarded behind some sort of "if (!found)" condition (i.e., only run in the
postmaster), it'll still crash. I think we just need to add some extra
notes to the docs and check for !IsUnderPostmaster, as discussed upthread.

I think v2 is fine because it is perfectly fine for a normal backend (
EXEC_BACKEND)
to call this function as long as it's processing the startup hook. The
goal is to prevent it
from being called outside of the startup hook.

Or... what if we just moved the request array to shared memory?

I guess that works also, if we want to maintain the existing behavior.
I am OK with this
as well, and I don't see anything wrong with v3.

FWIW, I got the tests discussed in [0]/messages/by-id/aLcPbyWLawp5_rdt@nathan commit ready and also included
tests for this
crash.

Attached is the patch with the tests. v4-0001 and v3-0001 are identical.
v4-0002 includes the tests.

I think we should commit these tests as well. If you think the tests should
be a separate thread, let me know.

--
Sami

[0]: /messages/by-id/aLcPbyWLawp5_rdt@nathan

#19Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#18)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Mon, Sep 08, 2025 at 02:47:26PM -0500, Sami Imseih wrote:

I think v2 is fine because it is perfectly fine for a normal backend
(EXEC_BACKEND) to call this function as long as it's processing the
startup hook. The goal is to prevent it from being called outside of the
startup hook.

I thought the goal was to prevent the crashes...

I think we should commit these tests as well. If you think the tests
should be a separate thread, let me know.

Thanks. I think we can discuss them here once we fix $SUBJECT. (BTW it
looks like you forgot to attach the patches.)

--
nathan

#20Sami Imseih
samimseih@gmail.com
In reply to: Nathan Bossart (#19)
Re: GetNamedLWLockTranche crashes on Windows in normal backend

On Mon, Sep 08, 2025 at 02:47:26PM -0500, Sami Imseih wrote:

I think v2 is fine because it is perfectly fine for a normal backend
(EXEC_BACKEND) to call this function as long as it's processing the
startup hook. The goal is to prevent it from being called outside of the
startup hook.

I thought the goal was to prevent the crashes...

yes it is. Ok, I see what I did wrong with my test that showed v2 working in
EXEC_BACKEND. when it was reentering the startup hook, it was skipping
the code under (!found) for shared memory existing. That is also where I
had the code to call GetNamedLWLockTranche. So, that's wrong
( which is what you also mentioned above) I moved
the GetNamedLWLockTranche outside of the (!found) block in the test.

I think we should commit these tests as well. If you think the tests
should be a separate thread, let me know.

Thanks. I think we can discuss them here once we fix $SUBJECT. (BTW it
looks like you forgot to attach the patches.)

oops. Attached now.

--
Sami

Attachments:

v4-0002-Tests-for-LWLock-tranche-registration-improvement.patchapplication/octet-stream; name=v4-0002-Tests-for-LWLock-tranche-registration-improvement.patchDownload+487-2
v4-0001-Move-named-LWLock-tranche-request-array-to-shared.patchapplication/octet-stream; name=v4-0001-Move-named-LWLock-tranche-request-array-to-shared.patchDownload+33-6
#21Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#20)
#22Nathan Bossart
nathandbossart@gmail.com
In reply to: Nathan Bossart (#21)
#23Nathan Bossart
nathandbossart@gmail.com
In reply to: Nathan Bossart (#22)
#24Sami Imseih
samimseih@gmail.com
In reply to: Nathan Bossart (#23)
#25Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#24)
#26Sami Imseih
samimseih@gmail.com
In reply to: Nathan Bossart (#25)
#27Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#26)
#28Sami Imseih
samimseih@gmail.com
In reply to: Nathan Bossart (#27)
#29Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#28)
#30Sami Imseih
samimseih@gmail.com
In reply to: Nathan Bossart (#29)
#31Alexander Lakhin
exclusion@gmail.com
In reply to: Nathan Bossart (#21)
#32Nathan Bossart
nathandbossart@gmail.com
In reply to: Alexander Lakhin (#31)
#33Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#30)