From 6403b444c11ceec91582e05ae412e029724aaa1a Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Sun, 25 Oct 2026 14:24:19 -0400 Subject: [PATCH v3 2/2] aio: Fix lost wakeups when waiting for another backend's buffer IO. A backend that saw BM_IO_IN_PROGRESS before io_wref was set slept on the buffer CV and was not woken once io_wref was set. With io_uring the IO could then never be reaped. Also wake submit_cv waiters after synchronous IO execution, which skips pgaio_submit_staged(). Reported-by: Tomas Barton Backpatch-through: 18 Discussion: https://postgr.es/m/19792-9da3b35b8f4b3e60%40postgresql.org --- src/backend/storage/aio/aio.c | 3 +++ src/backend/storage/buffer/bufmgr.c | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/src/backend/storage/aio/aio.c b/src/backend/storage/aio/aio.c index 9aa118a7592..bb823f00152 100644 --- a/src/backend/storage/aio/aio.c +++ b/src/backend/storage/aio/aio.c @@ -474,6 +474,9 @@ pgaio_io_stage(PgAioHandle *ioh, PgAioOp op) { pgaio_io_prepare_submit(ioh); pgaio_io_perform_synchronously(ioh); + + /* this IO never goes through pgaio_submit_staged() */ + ConditionVariableBroadcast(&pgaio_my_backend->submit_cv); } RESUME_INTERRUPTS(); diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index c17fc3590f0..a21a3926bf0 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -8410,8 +8410,17 @@ buffer_stage_common(PgAioHandle *ioh, bool is_write, bool is_temp) pg_atomic_unlocked_write_u64(&buf_hdr->state, buf_state); } else + { UnlockBufHdrExt(buf_hdr, buf_state, 0, 0, 1); + /* + * WaitIO() may have seen BM_IO_IN_PROGRESS before io_wref was + * set, and gone to sleep on the CV. Wake it up so it can wait + * via io_wref, instead of depending on us to complete the IO. + */ + ConditionVariableBroadcast(BufferDescriptorGetIOCV(buf_hdr)); + } + /* * Ensure the content lock that prevents buffer modifications while * the buffer is being written out is not released early due to an -- 2.37.1 (Apple Git-137.1)