Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f200e4

Browse files
author
Commitfest Bot
committed
[CF 6617] v3 - Fix a file descriptor leak when using io_method=io_uring
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/6617 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/5yehNM_HKke1aopIQHAYC6WoNA0KL1URLiE-1RSzjPkJwOKCI6qrUn0OXGKfMsiCsADCEgWvt6ueQJXQeNkFwY9Pyg3f0iC6x3cObcYldyA=@draescher.fr Author(s): Lucas DRAESCHER
2 parents f29299c + 3d5a586 commit 0f200e4

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/backend/storage/aio/aio_init.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ AioShmemRequest(void *arg)
172172
pgaio_method_ops->shmem_callbacks.request_fn(pgaio_method_ops->shmem_callbacks.opaque_arg);
173173
}
174174

175+
/*
176+
* Wrapper around pgaio_method_ops->shmem_cleanup to satisfy the
177+
* on_shmem_exit() callback signature.
178+
*/
179+
static void
180+
pgaio_shmem_cleanup(int code, Datum arg)
181+
{
182+
/*
183+
* No null check needed here; AioShmemInit only registers this callback
184+
* when shmem_cleanup is non-null.
185+
*/
186+
pgaio_method_ops->shmem_cleanup();
187+
}
188+
175189
/*
176190
* Initialize AIO shared memory during postmaster startup.
177191
*/
@@ -225,6 +239,10 @@ AioShmemInit(void *arg)
225239

226240
if (pgaio_method_ops->shmem_callbacks.init_fn)
227241
pgaio_method_ops->shmem_callbacks.init_fn(pgaio_method_ops->shmem_callbacks.opaque_arg);
242+
243+
/* Register callback to release any resources allocated above. */
244+
if (pgaio_method_ops->shmem_cleanup)
245+
on_shmem_exit(pgaio_shmem_cleanup, 0);
228246
}
229247

230248
static void

src/backend/storage/aio/method_io_uring.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
/* Entry points for IoMethodOps. */
5252
static void pgaio_uring_shmem_request(void *arg);
5353
static void pgaio_uring_shmem_init(void *arg);
54+
static void pgaio_uring_shmem_cleanup(void);
5455
static void pgaio_uring_init_backend(void);
5556
static int pgaio_uring_submit(uint16 num_staged_ios, PgAioHandle **staged_ios);
5657
static void pgaio_uring_wait_one(PgAioHandle *ioh, uint64 ref_generation);
@@ -72,6 +73,7 @@ const IoMethodOps pgaio_uring_ops = {
7273

7374
.shmem_callbacks.request_fn = pgaio_uring_shmem_request,
7475
.shmem_callbacks.init_fn = pgaio_uring_shmem_init,
76+
.shmem_cleanup = pgaio_uring_shmem_cleanup,
7577
.init_backend = pgaio_uring_init_backend,
7678

7779
.submit = pgaio_uring_submit,
@@ -403,6 +405,22 @@ pgaio_uring_shmem_init(void *arg)
403405
}
404406
}
405407

408+
static void
409+
pgaio_uring_shmem_cleanup(void)
410+
{
411+
if (pgaio_uring_contexts != NULL)
412+
{
413+
int TotalProcs = pgaio_uring_procs();
414+
415+
elog(DEBUG1, "cleaning up %d io_uring processes", TotalProcs);
416+
417+
for (int i = 0; i < TotalProcs; i++)
418+
io_uring_queue_exit(&pgaio_uring_contexts[i].io_uring_ring);
419+
420+
pgaio_uring_contexts = NULL;
421+
}
422+
}
423+
406424
static void
407425
pgaio_uring_init_backend(void)
408426
{

src/include/storage/aio_internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ typedef struct IoMethodOps
272272
/* global initialization */
273273
ShmemCallbacks shmem_callbacks;
274274

275+
/*
276+
* Clean up shared memory resources before shutdown. Called during shmem
277+
* exit. Optional.
278+
*/
279+
void (*shmem_cleanup) (void);
280+
275281
/*
276282
* Per-backend initialization. Optional.
277283
*/

0 commit comments

Comments
 (0)