Skip to content

Commit

Permalink
wasm-client: close client conn in a goroutine
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ViktorTigerstrom committed Feb 5, 2024
1 parent d8c9f92 commit 7665c9a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
27 changes: 22 additions & 5 deletions cmd/wasm-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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' ;
Expand Down

0 comments on commit 7665c9a

Please sign in to comment.