Skip to content

Commit

Permalink
Check if worker registration succeeded
Browse files Browse the repository at this point in the history
If `RegisterDynamicbackgroundworker` fails the returned handle will be
null, which will cause `WaitForBackgroundWorkerStartup` to fail with a
crash. This can typically happen if there is not enough slots in
`BackgroundWorkerData`. In that case, just return and do not call
`WaitForBackgroundWorkerStartup`.
  • Loading branch information
mkindahl committed Oct 30, 2023
1 parent 0aefc07 commit 98623ea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions .unreleased/fix_6240
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes: #6240 Check if worker registration succeeded
14 changes: 14 additions & 0 deletions test/src/bgw/scheduler_mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ ts_bgw_db_scheduler_test_run_and_wait_for_scheduler_finish(PG_FUNCTION_ARGS)
worker_handle = start_test_scheduler(PG_GETARG_INT32(0), GetUserId());
TestAssertTrue(worker_handle != NULL);

/*
* If RegisterDynamicbackgroundworker fails, worker_handle will be
* NULL. Since messages have already been printed in, just exit.
*/
if (!worker_handle)
PG_RETURN_VOID();

BgwHandleStatus status = WaitForBackgroundWorkerStartup(worker_handle, &pid);
TestAssertTrue(BGWH_STARTED == status);
if (status != BGWH_STARTED)
Expand All @@ -248,6 +255,13 @@ ts_bgw_db_scheduler_test_run(PG_FUNCTION_ARGS)
current_handle = start_test_scheduler(PG_GETARG_INT32(0), GetUserId());
MemoryContextSwitchTo(old_ctx);

/*
* If RegisterDynamicbackgroundworker fails, current_handle will be
* NULL. Since messages have already been printed in, just exit.
*/
if (!current_handle)
PG_RETURN_VOID();

status = WaitForBackgroundWorkerStartup(current_handle, &pid);
TestAssertTrue(BGWH_STARTED == status);
if (status != BGWH_STARTED)
Expand Down

0 comments on commit 98623ea

Please sign in to comment.