Skip to content
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

Rebuild LSP service, socket, and client on reconnect #201

Merged
merged 1 commit into from
Jan 11, 2024

Conversation

DavisVaughan
Copy link
Contributor

Follow up to #193

I accidentally introduced a bug related to UI refreshes with CMD + R. If you run that with an active R session, then you should get a panic related to the LSP (something about unwrapping a None).


It turns out that we can't move the LSP service, socket, and client to start_kernel(). That code will get run exactly 1 time per ark session lifetime, but the lsp_start() method will get called every time the UI is refreshed (i.e. CMD + R) (which prompts a new comm_open request). This means we really do need "fresh" instances of these objects after each refresh.

Most of the code from #193 related to these objects is now reverted, moving their creation back into start_lsp().

However, we now also use an r_task() to ship the Client over to RMain after each refresh.

R_MAIN actually should be started up by the time we get here to call this r_task(), because the Lsp::start() method that calls start_lsp() will wait for the kernel_init_tx notification that is sent out by the first r_read_console() iteration, and R_MAIN exists at that point. I'm convinced that even if it didn't exist yet, then r_task() would correctly block until the channel for communicating with the main R thread was set up, due to get_tasks_tx().

I did some testing and things like usethis::edit_r_profile() (which use the Client stored in R_MAIN) work as expected before and after a CMD + R refresh, which makes me somewhat confident that the client is getting refreshed properly.

Comment on lines +522 to +534
// Forward `client` along to `RMain`.
// This also updates an outdated `client` after a reconnect.
// `RMain` should be initialized by now, since the caller of this
// function waits to receive the init notification sent on
// `kernel_init_rx`. Even if it isn't, this should be okay because
// `r_task()` defensively blocks until its sender is initialized.
r_task({
let client = client.clone();
move || {
let main = RMain::get_mut();
main.set_lsp_client(client);
}
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main new bit is here

@DavisVaughan DavisVaughan merged commit 6a002e3 into main Jan 11, 2024
1 check passed
@DavisVaughan DavisVaughan deleted the fix/lsp-reconnect branch January 11, 2024 16:49
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.

2 participants