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

ruff server no longer hangs after shutdown #11222

Merged
merged 3 commits into from
May 3, 2024
Merged

Conversation

snowsignal
Copy link
Member

Summary

Fixes #11207.

The server would hang after handling a shutdown request on IoThreads::join() because a global sender (MESSENGER, used to send window/showMessage notifications) would remain allocated even after the event loop finished, which kept the writer I/O thread channel open.

To fix this, I've made a few structural changes to ruff server. I've wrapped the send/receive channels and thread join handle behind a new struct, Connection, which facilitates message sending and receiving, and also runs IoThreads::join() after the event loop finishes. To control the number of sender channels, the Connection wraps the sender channel in an Arc and only allows the creation of a wrapper type, ClientSender, which hold a weak reference to this Arc instead of direct channel access. The wrapper type implements the channel methods directly to prevent access to the inner channel (which would allow the channel to be cloned). ClientSender's function is analogous to WeakSender in tokio. Additionally, the receiver channel cannot be accessed directly - the Connection only exposes an iterator over it.

These changes will guarantee that all channels are closed before the I/O threads are joined.

Test Plan

Repeatedly open and close an editor utilizing ruff server while observing the task monitor. The net total amount of open ruff instances should be zero once all editor windows have closed.

The following logs should also appear after the server is shut down:

Screenshot 2024-04-30 at 3 56 22 PM

This can be tested on VS Code by changing the settings and then checking Output.

@snowsignal snowsignal added the server Related to the LSP server label Apr 30, 2024
@snowsignal snowsignal self-assigned this Apr 30, 2024
Copy link
Contributor

github-actions bot commented Apr 30, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

crates/ruff_server/src/server.rs Outdated Show resolved Hide resolved
crates/ruff_server/src/server.rs Show resolved Hide resolved
@snowsignal snowsignal enabled auto-merge (squash) May 2, 2024 03:22
@snowsignal snowsignal merged commit dfbeca5 into main May 3, 2024
19 checks passed
@snowsignal snowsignal deleted the jane/server/shutdown branch May 3, 2024 01:09
snowsignal added a commit that referenced this pull request May 5, 2024
## Summary

A follow-up to #11222. `ruff
server` stalls during shutdown with Neovim because after it receives an
exit notification and closes the I/O thread, it attempts to log a
success message to `stderr`. Removing this log statement fixes this
issue.

## Test Plan

Track the instances of `ruff` in the OS task manager as you open and
close Neovim. A new instance should appear when Neovim starts and it
should disappear once Neovim is closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
server Related to the LSP server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ruff server not closing with neovim instances
2 participants