[Bug Report + Patch] File descriptor leak when io_method=io_uring

Поиск
Список
Период
Сортировка
Искать

[Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Lucas DRAESCHER <git@draescher.fr>
Дата:
Вложения:
Hello Hackers,

I noticed a file descriptor leak when running PostgreSQL 18 with
io_method=io_uring. As far as I could tell, it only triggers when
the server restarts due to one of the backends being killed.

How to reproduce the issue:
- Setup a server configured with io_uring
- Check the number of open file descriptors:
    ls -la "/proc/$(head -1 $PGDATA/postmaster.pid)/fd/" | grep "io_uring" | wc -l
- SIGKILL a backend to trigger a server restart:
    kill -9 $(psql -XtA -U postgres -c "SELECT pid FROM pg_stat_activity WHERE backend_type = 'client backend' LIMIT 1")
- Check the number of open files descriptors again:
    Expected: FD count remains the same.
    Actual: FD count has doubled.

Tested on PostgreSQL 18.0, 18.1, 18.2, 18.3 and git master on the
following platforms:
- Ubuntu Server 24.04: Linux 6.8.0, liburing 2.5
- Exherbo Linux: Linux 6.16.12, liburing 2.12
- Fedora Linux: Linux 6.19.7, liburing 2.13-dev

From what I could gather, this happens because 
pgaio_uring_shmem_init() in 
src/backend/storage/aio/method_io_uring.c doesn't cleanup 
the allocated resources on exit.

I've attached a patch which registers an on_shmem_exit() callback 
to close the file descriptors on server exit. I took inspiration
from how src/backend/storage/aio/method_worker.c handles cleanup.

Regards,

Lucas.

Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
"cca5507" <cca5507@qq.com>
Дата:
Hi,

Thanks for the report! I can reproduce this bug on master and your patch fixes it.

> I've attached a patch which registers an on_shmem_exit() callback
> to close the file descriptors on server exit. I took inspiration
> from how src/backend/storage/aio/method_worker.c handles cleanup.

I also verify that only the postmaster will call this callback because all children
of the postmaster will reset postmaster's callback.

My another thought is that add a shmem_cleanup callback to IoMethodOps and
do cleanup in this callback. Not sure which is better.

--
Regards,
ChangAo Chen

Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Lucas DRAESCHER <git@draescher.fr>
Дата:
Вложения:
Hi ChangAo,

Thanks for the review!

I've attached v2, which adds a shmem_cleanup callback to
IoMethodOps, registered in AioShmemInit().

Like you, I don't know which approach I prefer since the
new callback currently has only one implementor. If no
other IO method is expected to need cleanup, the simpler
v1 approach may be preferable. Happy to hear your thoughts.

I've also added this patch to the commitfest app:
https://commitfest.postgresql.org/patch/6617/

Regards,
Lucas.

Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Lucas DRAESCHER <git@draescher.fr>
Дата:
Вложения:
I have attached v3 which rebases v2 on top of master. 

v1 still applies cleanly.

Regards,

Lucas

Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Laetitia Avrot <laetitia.avrot@gmail.com>
Дата:

Hi Lucas,

I reviewed v3 on Debian, kernel 6.17.13, liburing 2.14.

I reproduced the bug on unpatched master: fd count doubles from 142 to 284 after killing a backend. With the patch applied, the count stays stable. The fix works.

Build was clean, no new warnings. All 245 regression tests pass, make check-world passes, and all 39 TAP tests pass.

The code is clean and follows PostgreSQL conventions. The null guard before registering the callback is correct, the wrapper satisfies the on_shmem_exit() signature, and setting pgaio_uring_contexts to NULL after cleanup prevents a double-free.

One note for the committer: on_shmem_exit() callbacks fire in LIFO order, so this callback runs before anything registered earlier in AioShmemInit(), which is the safe direction. Worth confirming that no earlier-registered callback could invalidate pgaio_uring_contexts before this one runs.

On the v1 vs v2 design question: I favour v2. The Table AM precedent suggests PostgreSQL prefers clean interface boundaries even with a single implementor, and Andres Freund has been explicit about this in the past.

Marking as Ready for Committer.

Regards,

Lætitia


Le ven. 5 juin 2026 à 17:15, Lucas DRAESCHER <git@draescher.fr> a écrit :
I have attached v3 which rebases v2 on top of master.

v1 still applies cleanly.

Regards,

Lucas

Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Lætitia AVROT <laetitia.avrot@gmail.com>
Дата:
The following review has been posted through the commitfest application:
make installcheck-world:  tested, passed
Implements feature:       not tested
Spec compliant:           not tested
Documentation:            not tested

Hi Lucas,
I reviewed v3 on Debian, kernel 6.17.13, liburing 2.14.
I reproduced the bug on unpatched master: fd count doubles from 142 to 284 after killing a backend. With the patch applied, the count stays stable. The fix works.
Build was clean, no new warnings. All 245 regression tests pass, make check-world passes, and all 39 TAP tests pass.
The code is clean and follows PostgreSQL conventions. The null guard before registering the callback is correct, the wrapper satisfies the on_shmem_exit() signature, and setting pgaio_uring_contexts to NULL after cleanup prevents a double-free.
One note for the committer: on_shmem_exit() callbacks fire in LIFO order, so this callback runs before anything registered earlier in AioShmemInit(), which is the safe direction. Worth confirming that no earlier-registered callback could invalidate pgaio_uring_contexts before this one runs.
On the v1 vs v2 design question: I favour v2. The Table AM precedent suggests PostgreSQL prefers clean interface boundaries even with a single implementor, and Andres Freund has been explicit about this in the past.
Marking as Ready for Committer.
Regards,
Lætitia

Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Lucas DRAESCHER <git@draescher.fr>
Дата:
Ilmar, Lætitia,

Thank you for taking the time to review my patch!

> On the v1 vs v2 design question: I favour v2. 
> The Table AM precedent suggests PostgreSQL prefers 
> clean interface boundaries even with a single 
> implementor, and Andres Freund has been explicit 
> about this in the past.

Thanks for the feedback, I'm happy for v2 to be the
design we settle on.

Thanks again to both of you for your help.

Lucas.


Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Jakub Wartak <jakub.wartak@enterprisedb.com>
Дата:
On Wed, Sep 9, 2026 at 8:10 PM Lucas DRAESCHER  wrote:
[..]
> v4 addresses all these items. I have moved the CF entry to "Needs review".

Hi Lucas,

patch LGTM:
- the tests pass: PG_TEST_INITDB_EXTRA_OPTS="-c io_method=io_uring"
meson test -C build
- issue is not reproducible with the patch
- code looks good

As for backpatching, dunno: on one hand I was suspecting more people would be
affected so far, but it seems to be rare? (judging from EDB's tickets and
not many people in this thread). So for me, fixing this only in master
would be good enough (or maybe not that many people are using PG18+io_uring
and getting SIGSEGvs). Anyway, just some nano findings:

a. elog(DEBUG1, "cleaning up %d io_uring processes", TotalProcs)
   we are not cleaning "io_uring processes", but "io_uring instances"

b. Commit message could stress more that that it prevents io_uring fd leaks
   on restarts, that sooner or later may cause failure to self-heal sooner
   or later with symptomps like one of the following:
   FATAL:  could not setup io_uring queue: Too many open files
   FATAL:  could not setup io_uring queue: Cannot allocate memory

Perhaps, just ammend a little, drop v5 here and set to Ready for Commiter
and let's see what happens.

-J.


Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
Вложения:
On Thu, Sep 10, 2026 at 3:10 AM Lucas DRAESCHER  wrote:
> Alternatively, if maintaining a separate backpatch variant is undesirable,
> v1 is still available. It doesn't change the `IoMethodOps` struct at all,
> so the ABI question disappears.

I don't object to the v4 approach for master, but I still prefer v1 for
backpatching because it keeps the changes minimal and avoids changing
the IoMethodOps ABI.

I've attached updated v1-based patches. The changes are:
- Rename the cleanup function.
- Replace the NULL check with an assertion and add Assert(!IsUnderPostmaster).
- Remove the debug log message.

How about committing this v1-based fix and backpatching it to v18, then
considering a common cleanup interface separately for master if needed?

Regards,

-- 
Fujii Masao

Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Jakub Wartak <jakub.wartak@enterprisedb.com>
Дата:
On Tue, Nov 3, 2026 at 3:53 PM Fujii Masao  wrote:
>
> On Thu, Sep 10, 2026 at 3:10 AM Lucas DRAESCHER  wrote:
> > Alternatively, if maintaining a separate backpatch variant is undesirable,
> > v1 is still available. It doesn't change the `IoMethodOps` struct at all,
> > so the ABI question disappears.
>
> I don't object to the v4 approach for master, but I still prefer v1 for
> backpatching because it keeps the changes minimal and avoids changing
> the IoMethodOps ABI.
>
> I've attached updated v1-based patches. The changes are:
> - Rename the cleanup function.
> - Replace the NULL check with an assertion and add Assert(!IsUnderPostmaster).
> - Remove the debug log message.
>
> How about committing this v1-based fix and backpatching it to v18, then
> considering a common cleanup interface separately for master if needed?

Hi Fujii, thanks for taking a look, if You are willing to backpatch then v1
sounds better way better for endusers and just to give little more confidence,
I've checked both patces on REL_18_STABLE and master:

1) this passes: PG_TEST_INITDB_EXTRA_OPTS="-c io_method=io_uring" meson test
   -C build
2) code looks good and is even simpler
3) and yes uring fd leaks are gone with this after SIGSEGV

v18 and especially upcoming v19 should have this IMHO if possible.

-J.


Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
Hi Fujii-san, Lucas,

On Tue, Nov 3, 2026 at 10:53 PM Fujii Masao  wrote:
>
> On Thu, Sep 10, 2026 at 3:10 AM Lucas DRAESCHER  wrote:
> > Alternatively, if maintaining a separate backpatch variant is undesirable,
> > v1 is still available. It doesn't change the `IoMethodOps` struct at all,
> > so the ABI question disappears.
>
> I don't object to the v4 approach for master, but I still prefer v1 for
> backpatching because it keeps the changes minimal and avoids changing
> the IoMethodOps ABI.

+1

> I've attached updated v1-based patches. The changes are:
> - Rename the cleanup function.
> - Replace the NULL check with an assertion and add Assert(!IsUnderPostmaster).
> - Remove the debug log message.
>
> considering a common cleanup interface separately for master if needed?

Thanks for the patch. It looks good overall. I tested
v1-2(max_connections=20, max_worker_processes=4, and
max_wal_senders=0). After a backend crash restart, the postmaster's
io_uring fd count of patched remained at 48, that of unpatched doubled
up to 96.

AI raised one concern about pgaio_uring_cleanup().  It passes
io_uring_queue_exit() a ring descriptor from shared memory which could
be corrupted by a backend before crashing. To reproduce this, it set
one ring's ring_fd to the postmaster's Unix listen fd, then killed the
backend. On restart, the postmaster closed the listening socket, which
refused further attempts to connect. Not sure about the possibility of
triggering memory bugs like this in reality. For cautiousness, do we
need to make a local copy of the ring ds when creation and use them
while cleanup? I see the pattern in sysv_sema.c, where the postmaster
keeps its own array of semaphore-set IDs for cleanup.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.


Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Thu, Nov 5, 2026 at 4:12 PM Xuneng Zhou  wrote:
> For cautiousness, do we
> need to make a local copy of the ring ds when creation and use them
> while cleanup?

Yes, maybe. But I'm thinking of committing the FD leak fix first, then
addressing the use of postmaster-local copies of the ring descriptors
for cleanup in a separate patch.

Regards,

-- 
Fujii Masao


Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Thu, Nov 5, 2026 at 5:12 PM Fujii Masao  wrote:
> Yes, maybe. But I'm thinking of committing the FD leak fix first, then

I've pushed the patch. Thanks!

> addressing the use of postmaster-local copies of the ring descriptors
> for cleanup in a separate patch.

Let's keep working on this!

Regards,


-- 
Fujii Masao


Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
On Thu, Nov 5, 2026 at 5:12 PM Fujii Masao  wrote:
>
> On Thu, Nov 5, 2026 at 5:12 PM Fujii Masao  wrote:
> > Yes, maybe. But I'm thinking of committing the FD leak fix first, then
>
> I've pushed the patch. Thanks!
>
> > addressing the use of postmaster-local copies of the ring descriptors
> > for cleanup in a separate patch.
>
> Let's keep working on this!

Thanks for dealing with it! The separation seems ok to me. I could
write a follow-up patch or review it if you already did.

-- 
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.


Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Jakub Wartak <jakub.wartak@enterprisedb.com>
Дата:
On Thu, Nov 5, 2026 at 10:12 AM Fujii Masao  wrote:
>
> On Thu, Nov 5, 2026 at 5:12 PM Fujii Masao  wrote:
> > Yes, maybe. But I'm thinking of committing the FD leak fix first, then
>
> I've pushed the patch. Thanks!

Thank you!

-J.


Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
Вложения:
On Thu, Nov 5, 2026 at 6:25 PM Xuneng Zhou  wrote:
> I could
> write a follow-up patch or review it if you already did.

I tried to write a patch that keeps postmaster-local copies of the io_uring
descriptors and passes them to io_uring_queue_exit() for cleanup. Attached.

Per [1], io_uring_queue_exit() seems to use sq.kring_entries. Since this is
a pointer [2], simply copying the descriptor would leave it pointing into
shared memory. So, the patch also saves the pointed-to value locally.

But, I'm not sure this is the right approach... It relies on liburing's
implementation rather than a documented guarantee, so future liburing changes
would be really easily able to break it. This seems very fragile. Thought?
Is there a better approach?

Regards,

[1] https://github.com/axboe/liburing/blob/liburing-2.7/src/setup.c#L430-L445
[2] https://github.com/axboe/liburing/blob/liburing-2.7/src/include/liburing.h#L77-L83

-- 
Fujii Masao

Re: [Bug Report + Patch] File descriptor leak when io_method=io_uring

От:
Xuneng Zhou <xunengzhou@gmail.com>
Дата:
On Thu, Nov 5, 2026 at 10:37 PM Fujii Masao  wrote:
>
> On Thu, Nov 5, 2026 at 6:25 PM Xuneng Zhou  wrote:
> > I could
> > write a follow-up patch or review it if you already did.
>
> I tried to write a patch that keeps postmaster-local copies of the io_uring
> descriptors and passes them to io_uring_queue_exit() for cleanup. Attached.
>
> Per [1], io_uring_queue_exit() seems to use sq.kring_entries. Since this is
> a pointer [2], simply copying the descriptor would leave it pointing into
> shared memory. So, the patch also saves the pointed-to value locally.
>
> But, I'm not sure this is the right approach... It relies on liburing's
> implementation rather than a documented guarantee, so future liburing changes
> would be really easily able to break it. This seems very fragile. Thought?
> Is there a better approach?
>
> Regards,
>
> [1] https://github.com/axboe/liburing/blob/liburing-2.7/src/setup.c#L430-L445
> [2] https://github.com/axboe/liburing/blob/liburing-2.7/src/include/liburing.h#L77-L83

It does seem fragile. I tried to look for similar use cases in other
projects and see how they do it. However, it seems few share io_uring
rings across fork() the way we do. Most create rings per thread or per
process. So they won't bother doing the local-copy. It maybe worth
asking the liburing maintainer whether a copy of struct io_uring is
valid input for io_uring_queue_exit().

There's a somewhat hacky alternative. On kernel 6.5+ with liburing
2.5+, we create the rings with io_uring_queue_init_mem() in our own
shared memory, so noting maps the ring fds. For those rings, closing
the fd is what the teardown has to do, which is also
io_uring_queue_exit() does from liburign 2.8 on. In theory, we could
just make local copy of the those fd and just close them during
cleanup, without invoking into liburing. However, this is against the
convention of paring registeration and teardown.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.


FAQ