Skip to content

Commit

Permalink
feat: add custom killTimeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarpl committed Jul 26, 2022
1 parent 16c0427 commit ff9847c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface ThreadedClassConfig {
autoRestart?: boolean
/** If the process needs to restart, how long to wait for it to initalize, before failing. (default is 1000ms) */
restartTimeout?: number
/** If the process is being killed, how long to wait for it to terminate, before failing. (default is 1000ms) */
killTimeout?: number
/** Set to true to disable multi-threading, this might be useful when you want to disable multi-threading but keep the interface unchanged. */
disableMultithreading?: boolean
/** Set path to worker, used in browser */
Expand Down
7 changes: 5 additions & 2 deletions src/parent-process/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {
InitProps,
DEFAULT_CHILD_FREEZE_TIME,
DEFAULT_RESTART_TIMEOUT,
DEFAULT_KILL_TIMEOUT,
encodeArguments,
CallbackFunction,
Message,
ArgDefinition,
decodeArguments
decodeArguments,
} from '../shared/sharedApi'
import { ThreadedClassConfig, ThreadedClass, MemUsageReport, MemUsageReportInner } from '../api'
import { isBrowser, nodeSupportsWorkerThreads, browserSupportsWebWorkers } from '../shared/lib'
Expand Down Expand Up @@ -835,6 +836,8 @@ export class ThreadedClassManagerClassInternal extends EventEmitter {
delete this._children[child.id]
resolve()
} else {
const killTimeout = child.config.killTimeout ?? DEFAULT_KILL_TIMEOUT

child.process.once('close', () => {
if (!dontCleanUp) {
// Clean up:
Expand All @@ -850,7 +853,7 @@ export class ThreadedClassManagerClassInternal extends EventEmitter {
setTimeout(() => {
delete this._children[child.id]
reject(`Timeout: Kill child process "${child.id}"`)
},1000)
},killTimeout)
if (!child.isClosing) {
child.isClosing = true
child.process.kill()
Expand Down
1 change: 1 addition & 0 deletions src/shared/sharedApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ThreadedClassConfig } from '../api'

export const DEFAULT_CHILD_FREEZE_TIME = 1000 // how long to wait before considering a child to be unresponsive
export const DEFAULT_RESTART_TIMEOUT = 1000 // how long to wait for the child to come back after restart
export const DEFAULT_KILL_TIMEOUT = 1000 // how long to wait for the thread to close when terminating it

export type InitProps = Array<InitProp>
export enum InitPropType {
Expand Down

0 comments on commit ff9847c

Please sign in to comment.