Skip to content

Commit 5d195dc

Browse files
committed
Be more careful about barriers when releasing BackgroundWorkerSlots.
ForgetBackgroundWorker lacked any memory barrier at all, while BackgroundWorkerStateChange had one but unaccountably did additional manipulation of the slot after the barrier. AFAICS, the rule must be that the barrier is immediately before setting or clearing slot->in_use. It looks like back in 9.6 when ForgetBackgroundWorker was first written, there might have been some case for not needing a barrier there, but I'm not very convinced of that --- the fact that the load of bgw_notify_pid is in the caller doesn't seem to guarantee no memory ordering problem. So patch 9.6 too. It's likely that this doesn't fix any observable bug on Intel hardware, but machines with weaker memory ordering rules could have problems here. Discussion: https://postgr.es/m/4046084.1620244003@sss.pgh.pa.us
1 parent 5015d3c commit 5d195dc

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/backend/postmaster/bgworker.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,11 @@ BackgroundWorkerStateChange(void)
290290
* bgw_notify_pid completes before the store to in_use.
291291
*/
292292
notify_pid = slot->worker.bgw_notify_pid;
293-
pg_memory_barrier();
294293
slot->pid = 0;
294+
295+
pg_memory_barrier();
295296
slot->in_use = false;
297+
296298
if (notify_pid != 0)
297299
kill(notify_pid, SIGUSR1);
298300

@@ -378,6 +380,8 @@ BackgroundWorkerStateChange(void)
378380
* points to it. This convention allows deletion of workers during
379381
* searches of the worker list, and saves having to search the list again.
380382
*
383+
* Caller is responsible for notifying bgw_notify_pid, if appropriate.
384+
*
381385
* This function must be invoked only in the postmaster.
382386
*/
383387
void
@@ -390,6 +394,13 @@ ForgetBackgroundWorker(slist_mutable_iter *cur)
390394

391395
Assert(rw->rw_shmem_slot < max_worker_processes);
392396
slot = &BackgroundWorkerData->slot[rw->rw_shmem_slot];
397+
Assert(slot->in_use);
398+
399+
/*
400+
* This memory barrier might not be completely necessary, but let's be
401+
* sure.
402+
*/
403+
pg_memory_barrier();
393404
slot->in_use = false;
394405

395406
ereport(DEBUG1,

0 commit comments

Comments
 (0)