Skip to content

Fix WASM-client disconnection error #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2024

Conversation

ViktorTigerstrom
Copy link
Contributor

In this PR, we add a fix that solves the error that the wasm-client freezes when attempting to close the sockets when disconnecting.

I've also added a commit to add the gbn subsystem to the wasm-client's logger. This simplifies debugging errors quite a lot. Let me know if you don't think that commit is worth including though :)

@ViktorTigerstrom ViktorTigerstrom changed the title Fx WASM-client disconnection error Fix WASM-client disconnection error Feb 1, 2024
@ViktorTigerstrom ViktorTigerstrom force-pushed the 2024-02-fix-lnc-disconnection-error branch from 9c9e76d to d1d5807 Compare February 2, 2024 07:58
@ViktorTigerstrom ViktorTigerstrom requested review from jamaljsr and removed request for ellemouton February 2, 2024 07:58
@ViktorTigerstrom
Copy link
Contributor Author

Updated the PR with adding a callback that's executed once the disconnection has completed, so that the frontend only updates once we're fully disconnected.

I noticed that for checking wether we are connected or not, we instead use a ticker that checks the IsConnected func every 200ms to determine wether the connection setup has completed or not. If you'd like me to do the same for the disconnected version, I could of course do so, so let me know if I should change this :)!

Copy link
Member

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, LGTM 🎉

Copy link
Member

@jamaljsr jamaljsr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 🚀

I've confirmed the disconnect issue is now fixed.

My only request would be to have some way to disable the additional logs that are now being outputted. It logs 2 lines every second which adds a lot of noise that isn't particularly useful info for us app devs. It makes it harder to read through our own logs. Could a config param be used to disable this?

@ViktorTigerstrom
Copy link
Contributor Author

Thanks a lot for the reviews @guggero & @jamaljsr!

@jamaljsr, just wanted to check, is setting the debuglevel to info when you start the wasm-client an ok way for you app-devs to get rid of the extra gbn subsystem logs? I.e. with the example-server the log level is set to trace which is why the extra log lines are shown. https://github.com/lightninglabs/lightning-node-connect/blob/d8c9f92056e2b5a0c908514b2159d01738f7afb7/example/index.html#L54C14-L54C32

But in a real setting when using the wasm-client, I'd imagine that the debuglevel would be set to info, which removes the log extra lines you're referring to. Or does setting the debuglevel to info remove too much useful information for you?

@jamaljsr
Copy link
Member

jamaljsr commented Feb 2, 2024

But in a real setting when using the wasm-client, I'd imagine that the debuglevel would be set to info, which removes the log extra lines you're referring to. Or does setting the debuglevel to info remove too much useful information for you?

Ah yes, great suggestion about tweaking the debuglevel param. While setting it to info wouldn't be ideal because it produces no output at all, I was able to fiddle with it and find the optimal level of logging with debuglevel=debug,GOBN=info,GRPC=info. This is even better than what we had previously as it was a bit too verbose before. This is perfect, thanks! 🙌

Updated the PR with adding a callback that's executed once the disconnection has completed, so that the frontend only updates once we're fully disconnected.

I prefer the callback approach over polling, so it looks good to me. My only nitpick would be to rename disconnected to onDisconnect

@ViktorTigerstrom ViktorTigerstrom force-pushed the 2024-02-fix-lnc-disconnection-error branch from d1d5807 to ff53bad Compare February 2, 2024 21:50
@ViktorTigerstrom
Copy link
Contributor Author

Ah yes, great suggestion about tweaking the debuglevel param. While setting it to info wouldn't be ideal because it produces no output at all, I was able to fiddle with it and find the optimal level of logging with debuglevel=debug,GOBN=info,GRPC=info. This is even better than what we had previously as it was a bit too verbose before. This is perfect, thanks! 🙌

Awesome :rocket!

I prefer the callback approach over polling, so it looks good to me. My only nitpick would be to rename disconnected to onDisconnect

Great ok! I've renamed the callback to onDisconnect with the latest push. I saw that we also define some callbacks as config options instead:

'--onlocalprivcreate=' + namespace + '.onLocalKeyCreate',
'--onremotekeyreceive=' + namespace + '.onRemoteKeyReceive',
'--onauthdata=' + namespace + '.onAuthData',

If you'd like me to do the same for the onDisconnect callback let me know, but I found this approach I've implemented in the PR a bit cleaner.

Copy link
Member

@jamaljsr jamaljsr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK LGTM 🚀

If you'd like me to do the same for the onDisconnect callback let me know, but I found this approach I've implemented in the PR a bit cleaner.

I like this approach as well.

Comment on lines 295 to 303
go func() {
if err := w.lndConn.Close(); err != nil {
log.Errorf("Error closing RPC connection: %v", err)
}
w.lndConn = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it maybe worth using a wait group just to ensure that things do actually complete?

Copy link
Contributor Author

@ViktorTigerstrom ViktorTigerstrom Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review and suggestion @ellemouton!

Unfortunately I did try that before, but using a WaitGroup and then Waiting for the WaitGroup at the end of the Disconnect function makes the same problem that existed prior to the fix occur. The main thread then blocks on the Wait and for some reason it never picks up that the Websocket is now Hard closed by the new updates to the grpc dependency (introduced in v0.18.1). I think this is because this is in a JavaScript environment, and the main thread therefore never picks up any changes to the Javascript objects as it's blocking. So the WaitGroup Wait would never complete.

I therefore chose the alternative to signal the completion by a callback instead, as that unblocks the main thread, and makes it pick up the hard close of the Websocket. Hope that makes sense!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense! thanks for the explanation :)

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.
@ViktorTigerstrom ViktorTigerstrom force-pushed the 2024-02-fix-lnc-disconnection-error branch from ff53bad to 58a5902 Compare February 5, 2024 10:37
@ViktorTigerstrom
Copy link
Contributor Author

Just added some docs to the Disconnect to include info that the function expects that the onDisconnect is passed as an argument. I also reformatted the line breaks in the function.

Copy link
Member

@ellemouton ellemouton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK, LGTM!

Great investigation on this 👏 🏆

@ellemouton ellemouton merged commit f135fdc into master Feb 5, 2024
@ellemouton ellemouton deleted the 2024-02-fix-lnc-disconnection-error branch February 5, 2024 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants