Skip to content

Commit f135fdc

Browse files
authored
Merge pull request #103 from lightninglabs/2024-02-fix-lnc-disconnection-error
Fix WASM-client disconnection error
2 parents d8c9f92 + 58a5902 commit f135fdc

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

cmd/wasm-client/log.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package main
44

55
import (
66
"github.com/btcsuite/btclog"
7+
"github.com/lightninglabs/lightning-node-connect/gbn"
78
"github.com/lightninglabs/lightning-node-connect/mailbox"
89
"github.com/lightningnetwork/lnd"
910
"github.com/lightningnetwork/lnd/build"
@@ -25,6 +26,7 @@ func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) {
2526

2627
lnd.SetSubLogger(root, Subsystem, log)
2728
lnd.AddSubLogger(root, mailbox.Subsystem, intercept, mailbox.UseLogger)
29+
lnd.AddSubLogger(root, gbn.Subsystem, intercept, gbn.UseLogger)
2830

2931
grpclog.SetLoggerV2(NewGrpcLogLogger(root, intercept, "GRPC"))
3032
}

cmd/wasm-client/main.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,29 @@ func (w *wasmClient) IsConnected(_ js.Value, _ []js.Value) interface{} {
288288
return js.ValueOf(w.lndConn != nil)
289289
}
290290

291-
func (w *wasmClient) Disconnect(_ js.Value, _ []js.Value) interface{} {
291+
// Disconnect disconnects the client, and closes the connection.
292+
// The first argument passed should be a onDisconnect callback, which will be
293+
// invoked once the client has disconnected.
294+
func (w *wasmClient) Disconnect(_ js.Value, args []js.Value) interface{} {
292295
if w.lndConn != nil {
293-
if err := w.lndConn.Close(); err != nil {
294-
log.Errorf("Error closing RPC connection: %v", err)
295-
}
296-
w.lndConn = nil
296+
// We launch the closure of the connection in a goroutine to
297+
// avoid that the JS websocket freezes and blocks while closing.
298+
go func() {
299+
if err := w.lndConn.Close(); err != nil {
300+
log.Errorf("Error closing RPC connection: %v",
301+
err)
302+
}
303+
w.lndConn = nil
304+
305+
// We expect the first arg to be the onDisconnect
306+
// callback
307+
if len(args) > 0 && args[0].Type() == js.TypeFunction {
308+
callback := args[0]
309+
310+
// Call the onDisconnect callback.
311+
callback.Invoke()
312+
}
313+
}()
297314
}
298315

299316
return nil

example/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@
9898
}
9999

100100
async function disconnect() {
101-
window[namespace].wasmClientDisconnect();
101+
window[namespace].wasmClientDisconnect(onDisconnect);
102+
}
102103

104+
function onDisconnect() {
103105
document.getElementById('disconnectBtn').disabled = true;
104106
document.getElementById('reconnectBtn').disabled = false;
105107
document.getElementById('ready').style.display= 'none' ;

0 commit comments

Comments
 (0)