Skip to content

Commit 77dfa60

Browse files
committed
fix: re-raise signal in signal handler
This makes sure the process also terminates with the correct exit code if DASH0_FLUSH_ON_SIGTERM_SIGINT is active and the process is terminated by SIGTERM or SIGNIT. Also: Add a bit more logging when DASH0_DEBUG is set.
1 parent 69aec3d commit 77dfa60

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/init.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,43 +176,56 @@ function createBootstrapSpanIfRequested() {
176176

177177
function installProcessExitHandlers() {
178178
if (hasOptedIn('DASH0_FLUSH_ON_SIGTERM_SIGINT')) {
179+
printDebugStdout('Installing process exit handler for SIGTERM/SIGINT.');
179180
['SIGTERM', 'SIGINT'].forEach(signal => {
180-
process.once(signal, onProcessExit.bind(null, true));
181+
process.once(signal, onProcessExit.bind(null, signal));
181182
});
182183
}
183184

184185
if (!hasOptedOut('DASH0_FLUSH_ON_EMPTY_EVENT_LOOP')) {
185-
process.once('beforeExit', onProcessExit.bind(null, false));
186+
printDebugStdout('Installing process exit handler.');
187+
process.once('beforeExit', onProcessExit.bind(null));
186188
}
187189
}
188190

189-
async function onProcessExit(callProcessExit: boolean) {
190-
await executePromiseWithTimeout(gracefulSdkShutdown(callProcessExit), 500, callProcessExit);
191+
async function onProcessExit(signal?: string) {
192+
if (signal) {
193+
printDebugStdout('Running process exit handler for signal:', signal);
194+
} else {
195+
printDebugStdout('Running process exit handler.');
196+
}
197+
await executePromiseWithTimeout(gracefulSdkShutdown(signal), 500, signal);
191198
}
192199

193-
async function gracefulSdkShutdown(callProcessExit: boolean) {
200+
async function gracefulSdkShutdown(signal?: string) {
194201
try {
195202
if (sdkShutdownHasBeenCalled) {
196-
if (callProcessExit) {
197-
process.exit(0);
203+
printDebugStdout('Ignoring repeated request for graceful SDK shutdown.');
204+
if (signal) {
205+
// re-raise the signal to exit the process
206+
printDebugStdout('Re-raising signal', signal);
207+
process.kill(process.pid, signal);
198208
}
199209
return;
200210
}
201211

212+
printDebugStdout('Triggering graceful SDK shutdown.');
202213
sdkShutdownHasBeenCalled = true;
203214
await sdk.shutdown();
204215

205216
printDebugStdout('OpenTelemetry SDK has been shut down successfully.');
206217
} catch (err) {
207218
console.error(logPrefix, 'Error shutting down the OpenTelemetry SDK:', err);
208219
} finally {
209-
if (callProcessExit) {
210-
process.exit(0);
220+
if (signal) {
221+
// re-raise the signal to exit the process
222+
printDebugStdout('Re-raising signal', signal);
223+
process.kill(process.pid, signal);
211224
}
212225
}
213226
}
214227

215-
function executePromiseWithTimeout(promise: Promise<any>, timeoutMillis: number, callProcessExit: boolean) {
228+
function executePromiseWithTimeout(promise: Promise<any>, timeoutMillis: number, signal?: string) {
216229
let setTimeoutId: NodeJS.Timeout;
217230
const timeoutPromise = new Promise(resolve => {
218231
setTimeoutId = setTimeout(() => {
@@ -228,8 +241,10 @@ function executePromiseWithTimeout(promise: Promise<any>, timeoutMillis: number,
228241
if (setTimeoutId) {
229242
clearTimeout(setTimeoutId);
230243
}
231-
if (callProcessExit) {
232-
process.exit(0);
244+
if (signal) {
245+
// re-raise the signal to exit the process
246+
printDebugStdout('Re-raising signal', signal);
247+
process.kill(process.pid, signal);
233248
}
234249
});
235250
}

0 commit comments

Comments
 (0)