Fix background workers not restarting with restart_after_crash = on

Started by Andrey Rudometovover 1 year ago2 messageshackers
Jump to latest
#1Andrey Rudometov
unlimitedhikari@gmail.com

Good day, hackers.

Reading through changes committed in master, I noticed that after
CleanupBackend/CleanupBackroundworker refactor background workers will fail
to
start again after postgres' restart with restart_after_crash = on.

The reason is CleanupBackend and HandleChildCrash not setting background
worker's
rw_pid to zero anymore, if backend, well, crashed and failed to call
shmem_exit
and mark PMChild slot as inactive via MarkPostmasterChildInactive.

Suggested solution is to finish CleanupBackend's background worker related
logic
even after treating the child process as crashed. In earlier versions
zeroing of
pids happen in HandleChildCrash anyway, so there should be no harm in doing
the same actions here.

For fast reproduction I used pg_prewarm extension, as it creates observable
bgworker
and is present in postgres tree, so tap test is easy to run.
--
best regards,
Andrey Rudometov

Attachments:

v1-0001-Reproduce-bgw-not-starting-after-restart_after_cr.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Reproduce-bgw-not-starting-after-restart_after_cr.patchDownload+179-1
v1-0002-Fix-bgw-not-starting-after-restart_after_crash.patchtext/x-patch; charset=US-ASCII; name=v1-0002-Fix-bgw-not-starting-after-restart_after_crash.patchDownload+1-2
#2Fujii Masao
masao.fujii@gmail.com
In reply to: Andrey Rudometov (#1)
Re: Fix background workers not restarting with restart_after_crash = on

On Wed, Jun 11, 2025 at 5:26 PM Andrey Rudometov
<unlimitedhikari@gmail.com> wrote:

Good day, hackers.

Reading through changes committed in master, I noticed that after
CleanupBackend/CleanupBackroundworker refactor background workers will fail to
start again after postgres' restart with restart_after_crash = on.

The reason is CleanupBackend and HandleChildCrash not setting background worker's
rw_pid to zero anymore, if backend, well, crashed and failed to call shmem_exit
and mark PMChild slot as inactive via MarkPostmasterChildInactive.

Suggested solution is to finish CleanupBackend's background worker related logic
even after treating the child process as crashed. In earlier versions zeroing of
pids happen in HandleChildCrash anyway, so there should be no harm in doing
the same actions here.

For fast reproduction I used pg_prewarm extension, as it creates observable bgworker
and is present in postgres tree, so tap test is easy to run.

Thanks for the report and patch! This same issue was also reported in
thread [1]/messages/by-id/tencent_E00A056B3953EE6440F0F40F80EC30427D09@qq.com, where there's ongoing discussion about how to address it.

Regards,

[1]: /messages/by-id/tencent_E00A056B3953EE6440F0F40F80EC30427D09@qq.com

--
Fujii Masao