Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signal handling improvements #329

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
58 changes: 35 additions & 23 deletions src/master/implementation.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@ let tsNodeAvailable: boolean | undefined

export const defaultPoolSize = cpus().length

interface Terminable {
terminate(this: Terminable): any
}

// Terminates the workers, empties the workers array, and possibly exits.
const onSignal = (workers: Terminable[], signal: string) => {
// worker.terminate() might return a Promise or might be synchronous. This async helper function
// creates a consistent interface.
const terminate = async (worker: Terminable) => worker.terminate()
Promise.all(workers.map(worker => terminate(worker).catch(() => {}))).then(() => {
// Adding a signal listener suppresses the default signal handling behavior. That default
// behavior must be replicated here, but only if the default behavior isn't intentionally
// suppressed by another signal listener. Unfortunately there is no robust way to determine
// whether the default behavior was intentionally suppressed, so a heuristic is used. (Note: The
// 'exit' event is not suitable for terminating workers because it is not emitted when the
// default signal handler terminates the process.)
if (process.listenerCount(signal) > 1) {
// Assume that one of the other signal listeners will take care of calling process.exit().
// This assumption breaks down if all of the other listeners are making the same assumption.
return
}
// Right now this is the only signal listener, so assume that this listener is to blame for
// inhibiting the default signal handler. (This assumption fails if the number of listeners
// changes during signal handling. This can happen if a listener was added by process.once().)
// Mimic the default behavior, which is to exit with a non-0 code.
process.exit(1)
rhansen marked this conversation as resolved.
Show resolved Hide resolved
})
workers.length = 0
}

function detectTsNode() {
if (typeof __non_webpack_require__ === "function") {
// Webpack build: => No ts-node required or possible
Expand Down Expand Up @@ -98,7 +128,7 @@ function initWorkerThreadsWorker(): ImplementationExport {
? __non_webpack_require__("worker_threads").Worker
: eval("require")("worker_threads").Worker

let allWorkers: Array<typeof NativeWorker> = []
const allWorkers: Array<typeof NativeWorker> = []

class Worker extends NativeWorker {
private mappedEventListeners: WeakMap<EventListener, EventListener>
Expand Down Expand Up @@ -139,18 +169,9 @@ function initWorkerThreadsWorker(): ImplementationExport {
}
}

const terminateWorkersAndMaster = () => {
// we should terminate all workers and then gracefully shutdown self process
Promise.all(allWorkers.map(worker => worker.terminate())).then(
() => process.exit(0),
() => process.exit(1),
)
allWorkers = []
}

// Take care to not leave orphaned processes behind. See #147.
process.on("SIGINT", () => terminateWorkersAndMaster())
process.on("SIGTERM", () => terminateWorkersAndMaster())
process.on("SIGINT", (signal) => onSignal(allWorkers, signal))
process.on("SIGTERM", (signal) => onSignal(allWorkers, signal))

class BlobWorker extends Worker {
constructor(blob: Uint8Array, options?: ThreadsWorkerOptions) {
Expand Down Expand Up @@ -219,19 +240,10 @@ function initTinyWorker(): ImplementationExport {
}
}

const terminateWorkersAndMaster = () => {
// we should terminate all workers and then gracefully shutdown self process
Promise.all(allWorkers.map(worker => worker.terminate())).then(
() => process.exit(0),
() => process.exit(1),
)
allWorkers = []
}

// Take care to not leave orphaned processes behind
// See <https://github.com/avoidwork/tiny-worker#faq>
process.on("SIGINT", () => terminateWorkersAndMaster())
process.on("SIGTERM", () => terminateWorkersAndMaster())
process.on("SIGINT", (signal) => onSignal(allWorkers, signal))
process.on("SIGTERM", (signal) => onSignal(allWorkers, signal))

class BlobWorker extends Worker {
constructor(blob: Uint8Array, options?: ThreadsWorkerOptions) {
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
],
"interface-over-type-literal": false,
"member-ordering": false,
"no-empty": [true, "allow-empty-functions"],
"no-implicit-dependencies": [
true,
["tiny-worker", "worker_threads"]
Expand Down