pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix race condition between shutdown and unstarted background wor
Date: Thu, 24 Dec 2020 22:01:16 +0000
Message-ID: <E1ksYfc-0006UN-0w@gemulon.postgresql.org> (raw)

Fix race condition between shutdown and unstarted background workers.

If a database shutdown (smart or fast) is commanded between the time
some process decides to request a new background worker and the time
that the postmaster can launch that worker, then nothing happens
because the postmaster won't launch any bgworkers once it's exited
PM_RUN state.  This is fine ... unless the requesting process is
waiting for that worker to finish (or even for it to start); in that
case the requestor is stuck, and only manual intervention will get us
to the point of being able to shut down.

To fix, cancel pending requests for workers when the postmaster sends
shutdown (SIGTERM) signals, and similarly cancel any new requests that
arrive after that point.  (We can optimize things slightly by only
doing the cancellation for workers that have waiters.)  To fit within
the existing bgworker APIs, the "cancel" is made to look like the
worker was started and immediately stopped, causing deregistration of
the bgworker entry.  Waiting processes would have to deal with
premature worker exit anyway, so this should introduce no bugs that
weren't there before.  We do have a side effect that registration
records for restartable bgworkers might disappear when theoretically
they should have remained in place; but since we're shutting down,
that shouldn't matter.

Back-patch to v10.  There might be value in putting this into 9.6
as well, but the management of bgworkers is a bit different there
(notably see 8ff518699) and I'm not convinced it's worth the effort
to validate the patch for that branch.

Discussion: https://postgr.es/m/661570.1608673226@sss.pgh.pa.us

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/7519bd16d1a2a2007b3604f6740611c0b75a6cd0

Modified Files
--------------
contrib/pg_prewarm/autoprewarm.c            |  7 +--
src/backend/postmaster/bgworker.c           | 77 ++++++++++++++++++++++++++---
src/backend/postmaster/postmaster.c         | 23 ++++++---
src/include/postmaster/bgworker_internals.h |  3 +-
4 files changed, 90 insertions(+), 20 deletions(-)



view thread (5+ messages)  latest in thread

Message-ID: <E1ksYfc-0006UN-0w@gemulon.postgresql.org>
Permalink:  ../E1ksYfc-0006UN-0w@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1ksYfc-0006UN-0w@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Fix race condition between shutdown and unstarted background wor
  In-Reply-To: <E1ksYfc-0006UN-0w@gemulon.postgresql.org>
  References: <E1ksYfc-0006UN-0w@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox