Fix resource leak when a FDW's ForeignAsyncRequest function fails
authorHeikki Linnakangas
Thu, 23 Nov 2023 11:30:13 +0000 (13:30 +0200)
committerHeikki Linnakangas
Thu, 23 Nov 2023 11:30:13 +0000 (13:30 +0200)
commit501cfd07dac6dde5f8dae2d00fcf65d42e2caf80
tree68bc925a4fb290e6c9514c6ea5751e966b3a2620
parent1b6da28e0668eb977dcab6987d192ddedf32b752
Fix resource leak when a FDW's ForeignAsyncRequest function fails

If an error is thrown after calling CreateWaitEventSet(), the memory
of a WaitEventSet is free'd as it's allocated in the short-lived
memory context, but the file descriptor (on epoll- or kqueue-based
systems) or handles (on Windows) that it contains are leaked.

Use PG_TRY-FINALLY to ensure it gets freed. (On master, I will apply a
better fix, using ResourceOwners to track the WaitEventSet, but that's
not backpatchable.)

The added test doesn't check for leaking resources, so it passed even
before this commit. But at least it covers the code path.

In the passing, fix misleading comment on what the 'nevents' argument
to WaitEventSetWait means.

Report by Alexander Lakhin, analysis and suggestion for the fix by Tom
Lane. Fixes bug #17828. Backpatch to v14 where async execution was
introduced, but master gets a different fix.

Discussion: https://www.postgresql.org/message-id/17828-122da8cba23236be@postgresql.org
Discussion: https://www.postgresql.org/message-id/472235.1678387869@sss.pgh.pa.us
contrib/postgres_fdw/expected/postgres_fdw.out
contrib/postgres_fdw/sql/postgres_fdw.sql
src/backend/executor/nodeAppend.c