Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aea5d29

Browse files
committed
Notify bgworker registrant after freeing worker slot.
Tom Lane observed buildfarm failures caused by the select_parallel regression test trying to launch new parallel queries before the worker slots used by the previous ones were freed. Try to fix this by having the postmaster free the worker slots before it sends the SIGUSR1 notifications to the registering process. This doesn't completely eliminate the possibility that the user backend might (correctly) observe the worker as dead before the slot is free, but I believe it should make the window significantly narrower. Patch by me, per complaint from Tom Lane. Reviewed by Amit Kapila. Discussion: http://postgr.es/m/[email protected]
1 parent 5a73e17 commit aea5d29

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/backend/postmaster/bgworker.c

+33
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,39 @@ ReportBackgroundWorkerPID(RegisteredBgWorker *rw)
429429
kill(rw->rw_worker.bgw_notify_pid, SIGUSR1);
430430
}
431431

432+
/*
433+
* Report that the PID of a background worker is now zero because a
434+
* previously-running background worker has exited.
435+
*
436+
* This function should only be called from the postmaster.
437+
*/
438+
void
439+
ReportBackgroundWorkerExit(slist_mutable_iter *cur)
440+
{
441+
RegisteredBgWorker *rw;
442+
BackgroundWorkerSlot *slot;
443+
444+
rw = slist_container(RegisteredBgWorker, rw_lnode, cur->cur);
445+
446+
Assert(rw->rw_shmem_slot < max_worker_processes);
447+
slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
448+
slot->pid = rw->rw_pid;
449+
450+
/*
451+
* If this worker is slated for deregistration, do that before notifying
452+
* the process which started it. Otherwise, if that process tries to
453+
* reuse the slot immediately, it might not be available yet. In theory
454+
* that could happen anyway if the process checks slot->pid at just the
455+
* wrong moment, but this makes the window narrower.
456+
*/
457+
if (rw->rw_terminate ||
458+
rw->rw_worker.bgw_restart_time == BGW_NEVER_RESTART)
459+
ForgetBackgroundWorker(cur);
460+
461+
if (rw->rw_worker.bgw_notify_pid != 0)
462+
kill(rw->rw_worker.bgw_notify_pid, SIGUSR1);
463+
}
464+
432465
/*
433466
* Cancel SIGUSR1 notifications for a PID belonging to an exiting backend.
434467
*

src/backend/postmaster/postmaster.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -3050,9 +3050,9 @@ CleanupBackgroundWorker(int pid,
30503050
int exitstatus) /* child's exit status */
30513051
{
30523052
char namebuf[MAXPGPATH];
3053-
slist_iter iter;
3053+
slist_mutable_iter iter;
30543054

3055-
slist_foreach(iter, &BackgroundWorkerList)
3055+
slist_foreach_modify(iter, &BackgroundWorkerList)
30563056
{
30573057
RegisteredBgWorker *rw;
30583058

@@ -3126,7 +3126,7 @@ CleanupBackgroundWorker(int pid,
31263126
rw->rw_backend = NULL;
31273127
rw->rw_pid = 0;
31283128
rw->rw_child_slot = 0;
3129-
ReportBackgroundWorkerPID(rw); /* report child death */
3129+
ReportBackgroundWorkerExit(&iter); /* report child death */
31303130

31313131
LogChildExit(EXIT_STATUS_0(exitstatus) ? DEBUG1 : LOG,
31323132
namebuf, pid, exitstatus);

src/include/postmaster/bgworker_internals.h

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern void BackgroundWorkerShmemInit(void);
4242
extern void BackgroundWorkerStateChange(void);
4343
extern void ForgetBackgroundWorker(slist_mutable_iter *cur);
4444
extern void ReportBackgroundWorkerPID(RegisteredBgWorker *);
45+
extern void ReportBackgroundWorkerExit(slist_mutable_iter *cur);
4546
extern void BackgroundWorkerStopNotifications(pid_t pid);
4647
extern void ResetBackgroundWorkerCrashTimes(void);
4748

0 commit comments

Comments
 (0)