Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cspell-dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ envl
esac
eventsource
execa
execvpe
expialidocious
extnamespace
extwlist
Expand Down Expand Up @@ -426,8 +427,12 @@ vcpu
VERSINFO
webfactory
WINDIR
WIFSIGNALED
WNOHANG
WTERMSIG
WININICHANGE
withfig
waitpid
wrapline
writeout
wubalubadubdub
Expand Down
41 changes: 27 additions & 14 deletions src/lib/analytics-telemetry/worker-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable n/no-process-exit */

import {awaitProcessShutdown, type ShutdownSignal} from '../process-shutdown.js'
import {
CLIError, spawnTelemetryWorker, telemetryDebug, TelemetryGlobal,
} from './telemetry-utils.js'
Expand Down Expand Up @@ -43,24 +44,36 @@ export function setupTelemetryHandlers(options: SetupTelemetryOptions): void {
}
})

process.on('SIGINT', () => {
// Spawn background process to send telemetry
const error: CLIError = Object.assign(new Error('Received SIGINT'), {
let shutdownStarted = false
const handleSignal = async (signal: ShutdownSignal): Promise<void> => {
if (shutdownStarted) return

shutdownStarted = true

// Snapshot the error before waiting so signal-time duration and context are preserved.
const error: CLIError = Object.assign(new Error(`Received ${signal}`), {
cliRunDuration: computeDuration(cliStartTime),
context: {
isTTY: process.stdin.isTTY,
},
...(signal === 'SIGINT' ? {context: {isTTY: process.stdin.isTTY}} : {}),
})

try {
await awaitProcessShutdown(signal)
} catch (shutdownError) {
telemetryDebug('Error waiting for process shutdown before sending signal telemetry: %s', shutdownError)
}

spawnTelemetryWorker(error)
process.exit(1)
})
// Let the coordinator replay distinct unowned signals after the shutdown barrier settles.
setImmediate(() => {
// eslint-disable-next-line unicorn/no-process-exit -- preserve the CLI's signal exit status
process.exit(1)
})
}

process.on('SIGINT', () => {
handleSignal('SIGINT').catch(error => telemetryDebug('Error handling SIGINT shutdown: %s', error))
})
process.on('SIGTERM', () => {
// Spawn background process to send telemetry
const error: CLIError = Object.assign(new Error('Received SIGTERM'), {
cliRunDuration: computeDuration(cliStartTime),
})
spawnTelemetryWorker(error)
process.exit(1)
handleSignal('SIGTERM').catch(error => telemetryDebug('Error handling SIGTERM shutdown: %s', error))
})
}
Loading
Loading