From a1aee39407c16600c660a0fd4f31e3312bb7ed83 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 c73954aee6d..c640b7e0c80 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" @@ -275,6 +276,25 @@ pgaio_uring_shmem_size(void) return sz; } +/* + * 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(bool first_time) { @@ -391,6 +411,8 @@ pgaio_uring_shmem_init(bool first_time) LWLockInitialize(&context->completion_lock, LWTRANCHE_AIO_URING_COMPLETION); } + + on_shmem_exit(pgaio_uring_cleanup, 0); } static void -- 2.56.0