Skip to content

Commit 7665c9a

Browse files
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.
1 parent d8c9f92 commit 7665c9a

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

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)