Skip to content

Commit bf740ce

Browse files
committed
Fix status reporting for terminated bgworkers that were never started.
Previously, GetBackgroundWorkerPid() would return BGWH_NOT_YET_STARTED if the slot used for the worker registration had not been reused by unrelated activity, and BGWH_STOPPED if it had. Either way, a process that had requested notification when the state of one of its background workers changed did not receive such notifications. Fix things so that GetBackgroundWorkerPid() always returns BGWH_STOPPED in this situation, so that we do not erroneously give waiters the impression that the worker will eventually be started; and send notifications just as we would if the process terminated after having been started, so that it's possible to wait for the postmaster to process a worker termination request without polling. Discovered by Amit Kapila during testing of parallel sequential scan. Analysis and fix by me. Back-patch to 9.4; there may not be anyone relying on this interface yet, but if anyone is, the new behavior is a clear improvement.
1 parent 417f78a commit bf740ce

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/backend/postmaster/bgworker.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,37 @@ BackgroundWorkerStateChange(void)
244244
rw->rw_terminate = true;
245245
if (rw->rw_pid != 0)
246246
kill(rw->rw_pid, SIGTERM);
247+
else
248+
{
249+
/* Report never-started, now-terminated worker as dead. */
250+
ReportBackgroundWorkerPID(rw);
251+
}
247252
}
248253
continue;
249254
}
250255

251-
/* If it's already flagged as do not restart, just release the slot. */
256+
/*
257+
* If the worker is marked for termination, we don't need to add it
258+
* to the registered workers list; we can just free the slot.
259+
* However, if bgw_notify_pid is set, the process that registered the
260+
* worker may need to know that we've processed the terminate request,
261+
* so be sure to signal it.
262+
*/
252263
if (slot->terminate)
253264
{
265+
int notify_pid;
266+
267+
/*
268+
* We need a memory barrier here to make sure that the load of
269+
* bgw_notify_pid completes before the store to in_use.
270+
*/
271+
notify_pid = slot->worker.bgw_notify_pid;
272+
pg_memory_barrier();
273+
slot->pid = 0;
254274
slot->in_use = false;
275+
if (notify_pid != 0)
276+
kill(notify_pid, SIGUSR1);
277+
255278
continue;
256279
}
257280

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy