Skip to content

Commit 0ad77f6

Browse files
committed
fix: destroy lnsocket instances after lightning RPC calls
- wrap `ln_rpc()` socket usage in a `try/finally` - call `destroy()` after each RPC, with `disconnect()` as a fallback - prevent leaked WASM/native `lnsocket` allocations from exhausting the Emscripten heap - fix production crashes like `RuntimeError: Aborted(OOM)` during `LNSocket` creation Closes: #29
1 parent 0f039b9 commit 0ad77f6

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/invoicing.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,17 @@ class PurpleInvoiceManager {
252252
async ln_rpc(args) {
253253
const { method, params } = args
254254
const ln = await PurpleInvoiceManager.LNSocket()
255-
ln.genkey()
256-
await ln.connect_and_init(this.nodeid, this.address)
257-
return await ln.rpc({ rune: this.rune, method, params })
255+
try {
256+
ln.genkey()
257+
await ln.connect_and_init(this.nodeid, this.address)
258+
return await ln.rpc({ rune: this.rune, method, params })
259+
} finally {
260+
if (typeof ln.destroy === 'function') {
261+
ln.destroy()
262+
} else if (typeof ln.disconnect === 'function') {
263+
ln.disconnect()
264+
}
265+
}
258266
}
259267

260268
// Convenience function to get the LNSocket instance, in a way that is easy to mock in tests

0 commit comments

Comments
 (0)