From ba27fd1af756af476d272d39ba0e975ce014fbe4 Mon Sep 17 00:00:00 2001 From: Lucas DRAESCHER Date: Tue, 17 Mar 2026 17:26:11 +0100 Subject: [PATCH v1-2] Fix io_uring resource leaks across crash restarts With io_method=io_uring and restart_after_crash enabled, the postmaster reinitializes shared memory after a backend crash. It created new io_uring instances without releasing its references to the old ones, causing resources to accumulate on each crash restart. Eventually, creating new instances could fail with "Too many open files" or "Cannot allocate memory", preventing the server from restarting. Fix this by registering an on_shmem_exit() callback to release the postmaster's references to the io_uring instances during shared memory cleanup, before creating new ones. This prevents resources from accumulating across crash restarts. Backpatch to v18, where io_method=io_uring was introduced. Reported-by: Lucas DRAESCHER Author: Lucas DRAESCHER Reviewed-by: ChangAo Chen Reviewed-by: Ilmar Yunusov Reviewed-by: Laetitia Avrot Reviewed-by: Jakub Wartak Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/zHN8Rb7syWYDBPUhCICxKLr_tQmFLg0T024irxVVb6RSSicZcqcA0eT73z2PQy7JmzzqnUzX0Hi3P2dPKRvmNx_jbAqSCLyqJvWjXEypyAI=@draescher.fr Backpatch-through: 18 --- src/backend/storage/aio/method_io_uring.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/backend/storage/aio/method_io_uring.c b/src/backend/storage/aio/method_io_uring.c index 3ffe5061a20..d788ad18547 100644 --- a/src/backend/storage/aio/method_io_uring.c +++ b/src/backend/storage/aio/method_io_uring.c @@ -37,6 +37,7 @@ #include "miscadmin.h" #include "storage/aio_internal.h" #include "storage/fd.h" +#include "storage/ipc.h" #include "storage/proc.h" #include "storage/shmem.h" #include "storage/lwlock.h" @@ -288,6 +289,25 @@ pgaio_uring_shmem_request(void *arg) ); } +/* + * on_shmem_exit() callback that releases the io_uring instances created by + * pgaio_uring_shmem_init(). Besides process exit, this is called when the + * postmaster reinitializes shared memory after a backend crash. + */ +static void +pgaio_uring_cleanup(int code, Datum arg) +{ + int TotalProcs = pgaio_uring_procs(); + + Assert(!IsUnderPostmaster); + Assert(pgaio_uring_contexts != NULL); + + for (int i = 0; i < TotalProcs; i++) + io_uring_queue_exit(&pgaio_uring_contexts[i].io_uring_ring); + + pgaio_uring_contexts = NULL; +} + static void pgaio_uring_shmem_init(void *arg) { @@ -401,6 +421,8 @@ pgaio_uring_shmem_init(void *arg) LWLockInitialize(&context->completion_lock, LWTRANCHE_AIO_URING_COMPLETION); } + + on_shmem_exit(pgaio_uring_cleanup, 0); } static void -- 2.56.0