pg.ddx.io pgsql-committers@postgresql.org mailing list archivehelp / color / mirror / Atom feed
pgsql: Fix race condition between shutdown and unstarted background wor 5+ messages / 1 participants [nested] [flat]
* pgsql: Fix race condition between shutdown and unstarted background wor @ 2020-12-24 22:01 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 5+ messages in thread From: Tom Lane @ 2020-12-24 22:01 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Fix race condition between shutdown and unstarted background wor @ 2020-12-24 22:01 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 5+ messages in thread From: Tom Lane @ 2020-12-24 22:01 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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 ------ REL_10_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/1a6b1c460b4fa6c8ae8419d48ec5b1446820e13e Modified Files -------------- src/backend/postmaster/bgworker.c | 77 ++++++++++++++++++++++++++--- src/backend/postmaster/postmaster.c | 23 ++++++--- src/include/postmaster/bgworker_internals.h | 3 +- 3 files changed, 89 insertions(+), 14 deletions(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Fix race condition between shutdown and unstarted background wor @ 2020-12-24 22:01 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 5+ messages in thread From: Tom Lane @ 2020-12-24 22:01 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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 ------ REL_12_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/3d8068edce13464dc30837f7c9e6483920fc53fd 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(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Fix race condition between shutdown and unstarted background wor @ 2020-12-24 22:01 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 5+ messages in thread From: Tom Lane @ 2020-12-24 22:01 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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 ------ REL_13_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/0217ad806637fed6b3bce759169724f31b66256d 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(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
* pgsql: Fix race condition between shutdown and unstarted background wor @ 2020-12-24 22:01 Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 5+ messages in thread From: Tom Lane @ 2020-12-24 22:01 UTC (permalink / raw) To: pgsql-committers@lists.postgresql.org 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 ------ REL_11_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/b99b6b9d6cac6d45e8652a3f15f0823ab91d51d1 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(-) ^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2020-12-24 22:01 UTC | newest] Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2020-12-24 22:01 pgsql: Fix race condition between shutdown and unstarted background wor Tom Lane <tgl@sss.pgh.pa.us> 2020-12-24 22:01 pgsql: Fix race condition between shutdown and unstarted background wor Tom Lane <tgl@sss.pgh.pa.us> 2020-12-24 22:01 pgsql: Fix race condition between shutdown and unstarted background wor Tom Lane <tgl@sss.pgh.pa.us> 2020-12-24 22:01 pgsql: Fix race condition between shutdown and unstarted background wor Tom Lane <tgl@sss.pgh.pa.us> 2020-12-24 22:01 pgsql: Fix race condition between shutdown and unstarted background wor Tom Lane <tgl@sss.pgh.pa.us>
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