Fix WaitEventSet resource leak in WaitLatchOrSocket().

This function would have the same issue we solved in commit 501cfd07d:
If an error is thrown after calling CreateWaitEventSet(), the file
descriptor (on epoll- or kqueue-based systems) or handles (on Windows)
that the WaitEventSet contains are leaked.

Like that commit, use PG_TRY-PG_FINALLY (PG_TRY-PG_CATCH in v12) to make
sure the WaitEventSet is freed properly.

Back-patch to all supported versions, but as we do not have this issue
in HEAD (cf. commit 50c67c201), no need to apply this patch to it.

Discussion: https://postgr.es/m/CAPmGK16MqdDoD8oatp8SQWaEa4vS3nfQqDN_Sj9YRuu5J3Lj9g%40mail.gmail.com
This commit is contained in:
Etsuro Fujita 2024-04-11 19:05:05 +09:00
parent f5cee411a1
commit 01b01a77fe

View File

@ -395,6 +395,8 @@ WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock,
WaitEvent event; WaitEvent event;
WaitEventSet *set = CreateWaitEventSet(CurrentMemoryContext, 3); WaitEventSet *set = CreateWaitEventSet(CurrentMemoryContext, 3);
PG_TRY();
{
if (wakeEvents & WL_TIMEOUT) if (wakeEvents & WL_TIMEOUT)
Assert(timeout >= 0); Assert(timeout >= 0);
else else
@ -435,8 +437,12 @@ WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock,
WL_POSTMASTER_DEATH | WL_POSTMASTER_DEATH |
WL_SOCKET_MASK); WL_SOCKET_MASK);
} }
}
PG_FINALLY();
{
FreeWaitEventSet(set); FreeWaitEventSet(set);
}
PG_END_TRY();
return ret; return ret;
} }