Skip to content

Commit

Permalink
Merge pull request #401 from cosmos/fabo/323-handle-exceptions-in-chi…
Browse files Browse the repository at this point in the history
…ld-processes

Better error handling for child processes
  • Loading branch information
jbibla authored Jan 26, 2018
2 parents afdb6c9 + 2377ec0 commit 901d1cc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 33 deletions.
6 changes: 4 additions & 2 deletions app/src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ function startProcess (name, args, env) {
child.on('exit', (code) => !shuttingDown && log(`${name} exited with code ${code}`))
child.on('error', function (err) {
if (!(shuttingDown && err.code === 'ECONNRESET')) {
// Ignore ECONNRESET and re throw anything else
throw err
// if we throw errors here, they are not handled by the main process
console.error('[Uncaught Exception] Child', name, 'produced an unhandled exception:', err)
console.log('Shutting down UI')
shutdown()
}
})
return child
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@
"flatpickr": "^4.1.2",
"fs-extra": "^5.0.0",
"glob": "^7.1.2",
"jszip": "^3.1.5",
"interactjs": "^1.3.3",
"kill-port": "^1.0.0",
"jszip": "^3.1.5",
"moment": "^2.17.1",
"mousetrap": "^1.6.1",
"nib": "^1.1.2",
Expand Down
26 changes: 21 additions & 5 deletions tasks/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const config = require('../config')
const spawn = require('child_process').spawn
const path = require('path')
const killPort = require('kill-port')

let YELLOW = '\x1b[33m'
let BLUE = '\x1b[34m'
Expand All @@ -12,6 +11,24 @@ let END = '\x1b[0m'
let NPM_BIN = path.join(path.dirname(__dirname), 'node_modules', '.bin')
let PATH = `${NPM_BIN}:${process.env.PATH}`

// on windows some processes are not exiting when using child.kill so we use a windows specific comand
function cleanExitChild (child) {
return new Promise((resolve, reject) => {
var isWin = /^win/.test(process.platform)
if (!isWin) {
child.kill('SIGTERM')
} else {
var cp = require('child_process')
cp.exec('taskkill /PID ' + child.pid + ' /T /F', function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error)
}
})
}
child.on('exit', resolve)
})
}

function format (command, data, color) {
return color + command + END +
' ' + // Two space offset
Expand All @@ -31,7 +48,6 @@ function run (command, color, name, env) {
child.on('exit', code => {
console.log('exited', command, code)
})
process.on('exit', () => child.kill('SIGKILL'))
return child
}

Expand All @@ -49,7 +65,7 @@ function startRendererServer () {
}

module.exports = async function (networkPath) {
await startRendererServer()
let renderProcess = await startRendererServer()

console.log(`${BLUE}Starting electron...\n (network path: ${networkPath})\n${END}`)
let env = Object.assign({}, {
Expand All @@ -59,9 +75,9 @@ module.exports = async function (networkPath) {
let mainProcess = run('electron app/src/main/index.dev.js', BLUE, 'electron', env)

// terminate running processes on exit of main process
mainProcess.on('exit', code => {
mainProcess.on('exit', async code => {
await cleanExitChild(renderProcess)
// webpack-dev-server spins up an own process we have no access to. so we kill all processes on our port
killPort(config.port)
process.exit(0)
})
}
4 changes: 3 additions & 1 deletion tasks/testnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ async function main () {
}
}

main().catch(function (err) { throw err })
main().catch(function (err) {
console.error('Starting the application failed', err)
})
23 changes: 0 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2787,18 +2787,6 @@ exec-sh@^0.2.0:
dependencies:
merge "^1.1.3"

execa@^0.6.3:
version "0.6.3"
resolved "https://registry.yarnpkg.com/execa/-/execa-0.6.3.tgz#57b69a594f081759c69e5370f0d17b9cb11658fe"
dependencies:
cross-spawn "^5.0.1"
get-stream "^3.0.0"
is-stream "^1.1.0"
npm-run-path "^2.0.0"
p-finally "^1.0.0"
signal-exit "^3.0.0"
strip-eof "^1.0.0"

execa@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-0.7.0.tgz#944becd34cc41ee32a63a9faf27ad5a65fc59777"
Expand Down Expand Up @@ -3273,10 +3261,6 @@ get-stream@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14"

get-them-args@^1.2.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/get-them-args/-/get-them-args-1.3.1.tgz#ac1aeca013fda7db12591aaac129c58118e3daa8"

getpass@^0.1.1:
version "0.1.7"
resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa"
Expand Down Expand Up @@ -4576,13 +4560,6 @@ keyboardevents-areequal@^0.2.1:
version "0.2.2"
resolved "https://registry.yarnpkg.com/keyboardevents-areequal/-/keyboardevents-areequal-0.2.2.tgz#88191ec738ce9f7591c25e9056de928b40277194"

kill-port@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/kill-port/-/kill-port-1.0.0.tgz#b2c48d3048ea800d6620ff0d4b6f8ce91f644275"
dependencies:
execa "^0.6.3"
get-them-args "^1.2.1"

killable@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/killable/-/killable-1.0.0.tgz#da8b84bd47de5395878f95d64d02f2449fe05e6b"
Expand Down

0 comments on commit 901d1cc

Please sign in to comment.