Skip to content

Commit

Permalink
improve cleanup of scan worker process when exiting (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
mholtzman authored Oct 13, 2021
1 parent e1cc779 commit 76cda54
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions main/accounts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ class Accounts extends EventEmitter {
}

close () {
dataScanner.stop()
dataScanner.kill()
// usbDetect.stopMonitoring()
}
Expand Down
2 changes: 1 addition & 1 deletion main/externalData/balances/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function loadTokenBalances (chainId, address, tokens) {
const results = await multicall(chainId).call(calls.slice(batchStart, batchEnd))
return Object.entries(results.transformed)
} catch (e) {
log.error(`unable to load token balances (batch ${batchIndex}-${batchEnd}`, e)
log.error(`unable to load token balances (batch ${batchStart}-${batchEnd}`, e)
return []
}
})
Expand Down
20 changes: 16 additions & 4 deletions main/externalData/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,28 @@ function createWorker () {

scanWorker.on('error', err => {
if (err.code === 'ERR_IPC_CHANNEL_CLOSED') {
console.error('scan worker IPC channel closed! restarting worker')
log.error('scan worker IPC channel closed!')

kill()
setTimeout(restart, 1000 * 5)

if (heartbeat) {
log.info('restarting scan worker after IPC channel closed')
setTimeout(restart, 1000 * 5)
}
}

log.error(new Error(`scan worker error with code: ${err.code}`))
})

scanWorker.on('exit', code => {
log.warn(`scan worker exited with code ${code}, restarting worker`)
log.warn(`scan worker exited with code ${code}`)

kill()
setTimeout(restart, 1000 * 5)

if (heartbeat) {
log.info(`restarting scan worker after exiting with code ${code}`)
setTimeout(restart, 1000 * 5)
}
})

return scanWorker
Expand Down Expand Up @@ -232,6 +240,10 @@ function restart () {

function kill () {
if (scanWorker) {
const eventTypes = ['message', 'error', 'exit']

eventTypes.forEach(evt => scanWorker.removeAllListeners(evt))

scanWorker.kill()
scanWorker = null
}
Expand Down

0 comments on commit 76cda54

Please sign in to comment.