8000 run worker teardown on worker pipe end by rafapaezbas · Pull Request #610 · holepunchto/pear · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

run worker teardown on worker pipe end #610

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

Open
wants to merge 4 commits into
base: main
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
20 changes: 11 additions & 9 deletions lib/teardown.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
const Signal = require('bare-signals')
const safetyCatch = require('safety-catch')

module.exports = teardown

const handlers = []

let exiting = false
Expand All @@ -12,22 +10,22 @@ const sigint = new Signal('SIGINT')
const sigterm = new Signal('SIGTERM')

sigint
.on('signal', () => {
.on('signal', (signum) => {
sigint.stop()
sigint.unref()
Bare.exitCode = 130
onexit()
onexit(() => { Signal.send(signum, Bare.pid) })
})

sigterm
.on('signal', () => {
.on('signal', (signum) => {
sigterm.stop()
sigterm.unref()
Bare.exitCode = 143
onexit()
onexit(() => { Signal.send(signum, Bare.pid) })
})

function onexit () {
function onexit (ondone) {
if (exiting) return
exiting = true

Expand All @@ -40,11 +38,13 @@ function onexit () {
order[order.length - 1].push(h)
}

loop()
const loopdown = loop()

if (ondone) loopdown.finally(ondone)

function loop () {
if (!order.length) return
Promise.allSettled(order.pop().map(run)).then(loop, loop)
return Promise.allSettled(order.pop().map(run)).then(loop, loop)
}
}

Expand Down Expand Up @@ -82,3 +82,5 @@ function teardown (fn, position = 0) {
if (!handlers.length) cleanup()
}
}

module.exports = teardown
6 changes: 6 additions & 0 deletions
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const { isElectronRenderer, isWindows, isBare } = require('which-runtime')
const fs = isBare ? require('bare-fs') : require('fs')
const signals = isBare ? require('bare-signals') : null
const teardown = isBare ? require('./teardown') : (fn) => fn()
const { spawn } = isBare ? require('bare-subprocess') : require('child_process')
const { command } = require('paparam')
Expand Down Expand Up @@ -73,6 +74,11 @@ class Worker {
const pipe = new Pipe(fd)
pipe.on('end', () => {
teardown(() => pipe.end(), Number.MAX_SAFE_INTEGER)
if (isBare) {
if (!Bare.exiting) signals.send('SIGTERM', Bare.pid)
} else {
global.process.kill(global.process.pid, 'SIGTERM')
}
})
this.#pipe = pipe
pipe.once('close', () => {
Expand Down
Loading
0