Browse Source

fix(grpc): ensure full disconnect on window close

Do not try to send incoming data from GRPC streams to the main window
after the window has been closed.

Fix #677
renovate/lint-staged-8.x
Tom Kirkpatrick 7 years ago
parent
commit
114151f5f6
No known key found for this signature in database GPG Key ID: 72203A8EC5967EA8
  1. 9
      app/lib/lnd/lightning.js
  2. 23
      app/lib/lnd/subscribe/channelgraph.js
  3. 17
      app/lib/lnd/subscribe/invoices.js
  4. 17
      app/lib/lnd/subscribe/transactions.js

9
app/lib/lnd/lightning.js

@ -89,22 +89,23 @@ class Lightning {
*/ */
subscribe(mainWindow) { subscribe(mainWindow) {
this.mainWindow = mainWindow this.mainWindow = mainWindow
this.subscriptions.channelGraph = subscribeToChannelGraph(this.mainWindow, this.lnd, mainLog)
this.subscriptions.invoices = subscribeToInvoices(this.mainWindow, this.lnd, mainLog) this.subscriptions.channelGraph = subscribeToChannelGraph.call(this)
this.subscriptions.transactions = subscribeToTransactions(this.mainWindow, this.lnd, mainLog) this.subscriptions.invoices = subscribeToInvoices.call(this)
this.subscriptions.transactions = subscribeToTransactions.call(this)
} }
/** /**
* Unsubscribe from all bi-directional streams. * Unsubscribe from all bi-directional streams.
*/ */
unsubscribe() { unsubscribe() {
this.mainWindow = null
Object.keys(this.subscriptions).forEach(subscription => { Object.keys(this.subscriptions).forEach(subscription => {
if (this.subscriptions[subscription]) { if (this.subscriptions[subscription]) {
this.subscriptions[subscription].cancel() this.subscriptions[subscription].cancel()
this.subscriptions[subscription] = null this.subscriptions[subscription] = null
} }
}) })
this.mainWindow = null
} }
} }

23
app/lib/lnd/subscribe/channelgraph.js

@ -1,14 +1,21 @@
import { status } from 'grpc' import { status } from 'grpc'
import { mainLog } from '../../utils/log'
export default function subscribeToChannelGraph(mainWindow, lnd, log) { export default function subscribeToChannelGraph() {
const call = lnd.subscribeChannelGraph({}) const call = this.lnd.subscribeChannelGraph({})
call.on('data', channelGraphData => mainWindow.send('channelGraphData', { channelGraphData })) call.on('data', channelGraphData => {
call.on('end', () => log.info('end')) if (this.mainWindow) {
call.on('error', error => error.code !== status.CANCELLED && log.error(error)) this.mainWindow.send('channelGraphData', { channelGraphData })
call.on('status', channelGraphStatus => }
mainWindow.send('channelGraphStatus', { channelGraphStatus }) })
) call.on('end', () => mainLog.info('end'))
call.on('error', error => error.code !== status.CANCELLED && mainLog.error(error))
call.on('status', channelGraphStatus => {
if (this.mainWindow) {
this.mainWindow.send('channelGraphStatus', { channelGraphStatus })
}
})
return call return call
} }

17
app/lib/lnd/subscribe/invoices.js

@ -1,15 +1,18 @@
import { status } from 'grpc' import { status } from 'grpc'
import { mainLog } from '../../utils/log'
export default function subscribeToInvoices(mainWindow, lnd, log) { export default function subscribeToInvoices() {
const call = lnd.subscribeInvoices({}) const call = this.lnd.subscribeInvoices({})
call.on('data', invoice => { call.on('data', invoice => {
log.info('INVOICE:', invoice) mainLog.info('INVOICE:', invoice)
mainWindow.send('invoiceUpdate', { invoice }) if (this.mainWindow) {
this.mainWindow.send('invoiceUpdate', { invoice })
}
}) })
call.on('end', () => log.info('end')) call.on('end', () => mainLog.info('end'))
call.on('error', error => error.code !== status.CANCELLED && log.error(error)) call.on('error', error => error.code !== status.CANCELLED && mainLog.error(error))
call.on('status', status => log.info('INVOICE STATUS:', status)) call.on('status', status => mainLog.info('INVOICE STATUS:', status))
return call return call
} }

17
app/lib/lnd/subscribe/transactions.js

@ -1,15 +1,18 @@
import { status } from 'grpc' import { status } from 'grpc'
import { mainLog } from '../../utils/log'
export default function subscribeToTransactions(mainWindow, lnd, log) { export default function subscribeToTransactions() {
const call = lnd.subscribeTransactions({}) const call = this.lnd.subscribeTransactions({})
call.on('data', transaction => { call.on('data', transaction => {
log.info('TRANSACTION:', transaction) mainLog.info('TRANSACTION:', transaction)
mainWindow.send('newTransaction', { transaction }) if (this.mainWindow) {
this.mainWindow.send('newTransaction', { transaction })
}
}) })
call.on('end', () => log.info('end')) call.on('end', () => mainLog.info('end'))
call.on('error', error => error.code !== status.CANCELLED && log.error(error)) call.on('error', error => error.code !== status.CANCELLED && mainLog.error(error))
call.on('status', status => log.info('TRANSACTION STATUS: ', status)) call.on('status', status => mainLog.info('TRANSACTION STATUS: ', status))
return call return call
} }

Loading…
Cancel
Save