From 7665c9a65a5feb38aeb6c2b64059a51b89d640c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Tigerstr=C3=B6m?= Date: Thu, 1 Feb 2024 23:21:23 +0200 Subject: [PATCH 1/2] wasm-client: close client conn in a goroutine Close the client connection in a goroutine. This fixes a bug that was introduced with the upgrade to grpc v0.18.1. The caused the websocket to freeze during closure when the client disconnected, and therefore the entire wasm-client would freeze. When closing the connection in a goroutine, the websocket is closed correctly. --- cmd/wasm-client/main.go | 27 ++++++++++++++++++++++----- example/index.html | 4 +++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/cmd/wasm-client/main.go b/cmd/wasm-client/main.go index c8111803..7d713634 100644 --- a/cmd/wasm-client/main.go +++ b/cmd/wasm-client/main.go @@ -288,12 +288,29 @@ func (w *wasmClient) IsConnected(_ js.Value, _ []js.Value) interface{} { return js.ValueOf(w.lndConn != nil) } -func (w *wasmClient) Disconnect(_ js.Value, _ []js.Value) interface{} { +// Disconnect disconnects the client, and closes the connection. +// The first argument passed should be a onDisconnect callback, which will be +// invoked once the client has disconnected. +func (w *wasmClient) Disconnect(_ js.Value, args []js.Value) interface{} { if w.lndConn != nil { - if err := w.lndConn.Close(); err != nil { - log.Errorf("Error closing RPC connection: %v", err) - } - w.lndConn = nil + // We launch the closure of the connection in a goroutine to + // avoid that the JS websocket freezes and blocks while closing. + go func() { + if err := w.lndConn.Close(); err != nil { + log.Errorf("Error closing RPC connection: %v", + err) + } + w.lndConn = nil + + // We expect the first arg to be the onDisconnect + // callback + if len(args) > 0 && args[0].Type() == js.TypeFunction { + callback := args[0] + + // Call the onDisconnect callback. + callback.Invoke() + } + }() } return nil diff --git a/example/index.html b/example/index.html index 10f3f6fb..242fef4d 100644 --- a/example/index.html +++ b/example/index.html @@ -98,8 +98,10 @@ } async function disconnect() { - window[namespace].wasmClientDisconnect(); + window[namespace].wasmClientDisconnect(onDisconnect); + } + function onDisconnect() { document.getElementById('disconnectBtn').disabled = true; document.getElementById('reconnectBtn').disabled = false; document.getElementById('ready').style.display= 'none' ; From 58a590281ce2ad21913205f673107f5bb10c4409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Tigerstr=C3=B6m?= Date: Thu, 1 Feb 2024 23:19:54 +0200 Subject: [PATCH 2/2] wasm-client: add gbn subsystem to logger --- cmd/wasm-client/log.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/wasm-client/log.go b/cmd/wasm-client/log.go index 1b4f31ad..f699750d 100644 --- a/cmd/wasm-client/log.go +++ b/cmd/wasm-client/log.go @@ -4,6 +4,7 @@ package main import ( "github.com/btcsuite/btclog" + "github.com/lightninglabs/lightning-node-connect/gbn" "github.com/lightninglabs/lightning-node-connect/mailbox" "github.com/lightningnetwork/lnd" "github.com/lightningnetwork/lnd/build" @@ -25,6 +26,7 @@ func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) { lnd.SetSubLogger(root, Subsystem, log) lnd.AddSubLogger(root, mailbox.Subsystem, intercept, mailbox.UseLogger) + lnd.AddSubLogger(root, gbn.Subsystem, intercept, gbn.UseLogger) grpclog.SetLoggerV2(NewGrpcLogLogger(root, intercept, "GRPC")) }