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

Refactor SphinxManager and SubprocessSphinxClient #744

Merged
merged 6 commits into from
Feb 26, 2024

Conversation

alcarney
Copy link
Member

@alcarney alcarney commented Feb 25, 2024

This refactors the management of underlying Sphinx processes to be much simpler and hopefully much more robust.

  • The server will now index processes by configuration scope instead of Sphinx's srcdir
  • The sphinx client itself is awaitable,which allows consumers to await the client directly during creation, rather than having the manager try to act as an intermediary

See commit messages for 58d3727 and 340bea5 for full details

Closes #660

This adds a method that allows components to ask the configuration
system which configuration scope a given uri would fall into
By pushing the execution of async configuration change handlers into
`asyncio.Task` objects, we can make the `subscribe()` method
syncronous.

Not only does this not force users of the function to be async, we now
actually detect duplicate subscriptions and prevent unecessary calls
to the given handler.
This commit refactors the `SubprocessSphinxClient` so that it's
hopefully much more robust.

- There is no longer a separation between starting the client and
  creating a Sphinx application. There is now only a single `start()`
  method.

- The `start()` method no longer takes any arguments (the
  `SphinxConfig` is passed to the constructor instead) and the method
  returns `self`

- The client is now `await`-able. If required the `start()` method
  will be called and the client simply returned otherwise.

- There is now a `ClientState` enum which better explains the current
  condition of the client.

- The client is now an `EventSource` and will emit a `state-change`
  event whenever the state is changed.
This commit builds on the changes introduced to the
`SubprocessSphinxClient` to greatly simplify and improve the
robustness of managing the set of `SphinxClients` in use by the
language server.

There were a few drawbacks to the previous approach

- While await on the `_client_creating` task during client creation
  appeared to work, it still often led to the creation of duplicated
  clients.

- By not recognizing when a given configuration was broken, the
  manager would continually attempt to spawn client instances.

- It was not uncommon to enter an infinite loop of spawning clients!

The new system introduced in this commit *shouldn't* suffer from any
of the above issues.

- Instead of indexing clients by `src_uri`, they are instead indexed
  by the scope of the configuration governing the instance. While
  still not perfect, we should be able to accurately determine the
  relevant client for a given uri in more scenarios.

- To indicate that we are unable to create a client for a given
  config we now store a `None` in the clients dictionary - preventing
  any further attempts at spawning a client.

- Taking advantage of the fact clients are indexed by scope and that
  the client itself is `await`-able, we can have multiple consumers of
  the client awaiting on the client - preventing the issue of
  duplicated clients.

Currently, there is a quirk to the system where the first batch of
callers to `get_client` in a given scope will be told there is no
client. However, they have in fact "primed" the system by creating a
config subscription which will asynchronously populate the `clients`
dict with an unstarted client.

The next batch of callers will then discover that there is a client
instance available and `await` it, starting the underlying Sphinx
process.

Based on some initial testing this "quirk" may actually be desirable,
the `get_client` method is called often enough that I wasn't able to
notice a real delay in starting the Sphinx process up. It also has the
nice property that when restarting the server with multiple projects
open, the flood of `didOpen` notifications from the client is enough
to "prime" the system for each project, but the project itself is only
activated when working on the relevant file in the editor.

One final note, there are plenty of improvements still to be made -
error handling, config changes etc, but hopefully this is a solid base
we can start building on.
The tests now use the real thing and there does not seem to be much
benefit in trying to maintain this.
A number of tests have been marked as xfail or skipped as they will be
fixed for real in the next batch of changes
@alcarney alcarney merged commit 6badf21 into swyddfa:develop Feb 26, 2024
12 of 13 checks passed
@alcarney alcarney deleted the client-refactor branch February 26, 2024 20:37
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.

Sphinx manager still spawns duplicate Sphinx agents on server restart with multiple projects open
1 participant