Skip to content

Commit 053ce71

Browse files
committed
Fix a number of bugs
- set "errno" to 0 before calling strtol to avoid false errors - set "bgw_type" so that it doesn't contain garbage - sleep before sending a signal the first time (useful with kill) - reset the latch before waiting In addition, emit a log message after the worker has started. Per report from Wolfgang Brandl.
1 parent 814d4a1 commit 053ce71

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

pg_crash.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ _PG_init(void)
4747
worker.bgw_flags = BGWORKER_SHMEM_ACCESS;
4848
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
4949
worker.bgw_restart_time = 0;
50+
sprintf(worker.bgw_type, "crash worker");
5051
sprintf(worker.bgw_name, "crash worker");
5152
sprintf(worker.bgw_library_name, "pg_crash");
5253
sprintf(worker.bgw_function_name, "crash_worker_main");
@@ -105,7 +106,10 @@ crash_worker_main(Datum main_arg)
105106
{
106107
Size len = c - start;
107108
char *str = pnstrdup(start, len);
108-
long int nr = strtol(str, NULL, 10);
109+
long int nr;
110+
111+
errno = 0;
112+
nr = strtol(str, NULL, 10);
109113

110114
if (errno != 0)
111115
ereport(ERROR,
@@ -126,12 +130,27 @@ crash_worker_main(Datum main_arg)
126130
ereport(ERROR, (errmsg("no signals specified")));
127131
}
128132

133+
ereport(LOG,
134+
(errmsg("pg_crash background worker started, crash.delay = %d, crash.signals = '%s'",
135+
signal_delay, crash_signals)));
136+
129137
for (;;)
130138
{
131139
int i, j;
132140
int rc;
133141
int signal;
134142

143+
/* wait for signal_delay seconds */
144+
ResetLatch(MyLatch);
145+
146+
rc = WaitLatch(MyLatch, WL_LATCH_SET | WL_POSTMASTER_DEATH |
147+
WL_TIMEOUT, signal_delay * 1000L, 0);
148+
if (rc & WL_POSTMASTER_DEATH)
149+
break;
150+
151+
if (got_sigterm)
152+
break;
153+
135154
/* Select signal. */
136155
n = random() % list_length(signals);
137156
signal = list_nth_int(signals, n);
@@ -160,18 +179,6 @@ crash_worker_main(Datum main_arg)
160179
}
161180
j++;
162181
}
163-
164182
}
165-
166-
rc = WaitLatch(MyLatch, WL_LATCH_SET | WL_POSTMASTER_DEATH |
167-
WL_TIMEOUT, signal_delay * 1000L, 0);
168-
if (rc & WL_POSTMASTER_DEATH)
169-
break;
170-
171-
if (got_sigterm)
172-
break;
173-
174-
ResetLatch(MyLatch);
175183
}
176-
177184
}

0 commit comments

Comments
 (0)