aio: Fix crash potential for pg_aios views due to late state update
authorAndres Freund <andres@anarazel.de>
Fri, 25 Apr 2025 16:03:41 +0000 (12:03 -0400)
committerAndres Freund <andres@anarazel.de>
Fri, 25 Apr 2025 17:31:13 +0000 (13:31 -0400)
commit0d9114b7040d7503096e2897f4c856b17d461f6a
tree47ce4c3de14de234152deee6dd76d1ed1a216eeb
parent76d52e71659149d20d616c8a94c02793cedce066
aio: Fix crash potential for pg_aios views due to late state update

pgaio_io_reclaim() reset the fields in PgAioHandle before updating the state
to IDLE or incrementing the generation. For most things that's OK, but for
pg_get_aios() it is not - if it copied the PgAioHandle while fields were being
reset, we wouldn't detect that and could call
pgaio_io_get_target_description() with ioh->target == PGAIO_TID_INVALID,
leading to a crash.

Fix this issue by incrementing the generation and state earlier, before
resetting.

Also add an assertion to pgaio_io_get_target_description() for the target to
be valid - that'd have made this case a bit easier to debug. While at it,
add/update a few related assertions.

Author: Alexander Lakhin <exclusion@gmail.com>
Discussion: https://postgr.es/m/062daca9-dfad-4750-9da8-b13388301ad9@gmail.com
src/backend/storage/aio/aio.c
src/backend/storage/aio/aio_target.c