Skip to content

Commit

Permalink
fix(child_process): kill process with SIGKILL if stuck (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Sep 11, 2023
1 parent c5386d6 commit 6f6e382
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/runtime/process-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
TinypoolWorkerMessage,
} from '../common'

const __tinypool_worker_message__ = 'true'
const __tinypool_worker_message__ = true
const SIGKILL_TIMEOUT = 1000

export default class ProcessWorker implements TinypoolWorker {
name = 'ProcessWorker'
Expand All @@ -17,16 +18,35 @@ export default class ProcessWorker implements TinypoolWorker {
threadId!: number
port?: MessagePort
channel?: TinypoolChannel
waitForExit!: Promise<void>

initialize(options: Parameters<TinypoolWorker['initialize']>[0]) {
const __dirname = dirname(fileURLToPath(import.meta.url))

this.process = fork(resolve(__dirname, './entry/process.js'), options)
this.threadId = this.process.pid!

this.process.on('exit', this.onUnexpectedExit)
this.waitForExit = new Promise((r) => this.process.on('exit', r))
}

onUnexpectedExit = () => {
this.process.emit('error', new Error('Worker exited unexpectedly'))
}

async terminate() {
return this.process.kill()
this.process.off('exit', this.onUnexpectedExit)

const sigkillTimeout = setTimeout(
() => this.process.kill('SIGKILL'),
SIGKILL_TIMEOUT
)

this.process.kill()
await this.waitForExit

this.port?.close()
clearTimeout(sigkillTimeout)
}

setChannel(channel: TinypoolChannel) {
Expand Down Expand Up @@ -65,6 +85,11 @@ export default class ProcessWorker implements TinypoolWorker {

on(event: string, callback: (...args: any[]) => void) {
return this.process.on(event, (data: TinypoolWorkerMessage) => {
// All errors should be forwarded to the pool
if (event === 'error') {
return callback(data)
}

if (!data || !data.__tinypool_worker_message__) {
return this.channel?.postMessage(data)
}
Expand Down

0 comments on commit 6f6e382

Please sign in to comment.