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

correct worker process close sequence #814

Merged
merged 6 commits into from
Apr 21, 2022
Merged
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
3 changes: 3 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as Sentry from '@sentry/electron'
import React from 'react'
import ReactDOM from 'react-dom'
import Restore from 'react-restore'
Expand All @@ -9,6 +10,8 @@ import _store from './store'

import './flex'

Sentry.init({ dsn: 'https://7b09a85b26924609bef5882387e2c4dc@o1204372.ingest.sentry.io/6331069' })

// window.removeAllAccountsAndSigners = () => link.send('tray:removeAllAccountsAndSigners')

document.addEventListener('dragover', e => e.preventDefault())
Expand Down
50 changes: 28 additions & 22 deletions main/externalData/balances/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export default class BalancesWorkerController extends EventEmitter {

constructor () {
super()

const workerArgs = process.env.NODE_ENV === 'development' ? ['--inspect=127.0.0.1:9230'] : []
this.worker = fork(path.resolve(__dirname, 'worker.js'), workerArgs)
this.worker = fork(path.resolve(__dirname, 'worker.js'), [], { execArgv: workerArgs })

log.info('created balances worker, pid:', this.worker.pid)

Expand All @@ -56,38 +56,30 @@ export default class BalancesWorkerController extends EventEmitter {
}
})

this.worker.on('exit', code => {
const exitCode = code || this.worker.signalCode
log.warn(`balances worker exited with code ${exitCode}, pid: ${this.worker.pid}`)
this.close()
this.worker.on('close', (code, signal) => {
// emitted after exit or error and when all stdio streams are closed
log.warn(`balances worker exited with code ${code}, signal: ${signal}, pid: ${this.worker.pid}`)
this.worker.removeAllListeners()

this.emit('close')
this.removeAllListeners()
})

this.worker.on('disconnect', () => {
log.warn(`balances worker disconnected`)
this.close()
this.stopWorker()
})

this.worker.on('error', err => {
log.warn(`balances worker sent error, pid: ${this.worker.pid}`, err)
this.close()
this.stopWorker()
})
}

close () {
if (this.heartbeat) {
clearInterval(this.heartbeat)
this.heartbeat = undefined
}

this.worker.removeAllListeners()

const exitCode = this.worker.exitCode
const killed = this.worker.killed || this.worker.kill('SIGTERM')

log.info(`worker controller closed, exitCode: ${exitCode}, killed by controller: ${killed}`)
log.info(`closing worker controller`)

this.emit('close')
this.removeAllListeners()
this.stopWorker()
}

isRunning () {
Expand All @@ -106,12 +98,26 @@ export default class BalancesWorkerController extends EventEmitter {
this.sendCommandToWorker('tokenBalanceScan', [address, tokens, chains])
}

// private
private stopWorker () {
if (this.heartbeat) {
clearInterval(this.heartbeat)
this.heartbeat = undefined
}

this.worker.kill('SIGTERM')
}

private isWorkerReachable () {
return this.worker.connected && this.worker.channel && this.worker.listenerCount('error') > 0
}

// sending messages
private sendCommandToWorker (command: string, args: any[] = []) {
log.debug(`sending command ${command} to worker`)

try {
if (!this.worker.connected || !this.worker.channel) {
if (!this.isWorkerReachable()) {
log.error(`attempted to send command "${command}" to worker but worker cannot be reached!`)
return
}
Expand Down
4 changes: 3 additions & 1 deletion main/externalData/balances/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ export default function (store: Store) {
updateActiveBalances(address)
}, 0)

scan = setTimeout(() => scanForAddress(), 20 * 1000)
scan = setTimeout(() => {
if (workerController?.isRunning()) scanForAddress()
}, 20 * 1000)
}

runWhenReady(() => scanForAddress())
Expand Down
2 changes: 1 addition & 1 deletion main/externalData/balances/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import log from 'electron-log'

import ethProvider from 'eth-provider'

log.transports.console.format = '[scanWorker] {h}:{i}:{s} {text}'
log.transports.console.format = '[scanWorker] {h}:{i}:{s}.{ms} {text}'
log.transports.console.level = process.env.LOG_WORKER ? 'debug' : 'info'
log.transports.file.level = ['development', 'test'].includes(process.env.NODE_ENV) ? false : 'verbose'

Expand Down
9 changes: 9 additions & 0 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ function getCrashReportFields () {
}

Sentry.init({
// only use IPC from renderer process, not HTTP
ipcMode: Sentry.IPCMode.Classic,
dsn: 'https://7b09a85b26924609bef5882387e2c4dc@o1204372.ingest.sentry.io/6331069',
beforeSend: (evt) => {
return {
Expand Down Expand Up @@ -99,6 +101,13 @@ log.info('Electron: v' + process.versions.electron)
log.info('Node: v' + process.versions.node)

process.on('uncaughtException', (e) => {
Sentry.captureException(e)

if (e.code === 'EPIPE') {
log.error('uncaught EPIPE error', e)
return
}

if (e.code === 'EADDRINUSE') {
dialog.showErrorBox('Frame is already running', 'Frame is already running or another application is using port 1248.')
} else {
Expand Down