Skip to content

Add multi-session support to Positron - #2415

Merged
jmcphers merged 141 commits into
mainfrom
feature/runtime-sessions
Mar 12, 2024
Merged

jmcphers merged 141 commits into
mainfrom
feature/runtime-sessions

Conversation

@jmcphers

@jmcphers jmcphers commented Mar 8, 2024 •

Copy link
Copy Markdown
Collaborator

This change adds the infrastructure necessary to support multiple sessions per interpreter to Positron.

Its primary payload is support for making notebook sessions first-class citizens in Positron that can be connected to any of Positron's UI components, but it also addresses a number of other issues and is designed to support other multi-session scenarios.

Primarily addresses #1625.

Also addresses (directly or indirectly):
#1123
#2022
#1067
#634
#1456

Depends on (needs merge coordinated with) https://github.com/posit-dev/positron-python/pull/413.

Approach

Before this change, Positron had a single interface for language runtimes, which bundled two concepts into a class called LanguageRuntime:

  • a language runtime's metadata (its path, icon, ID, etc.)
  • controls for interacting with the runtime, such as starting it, stopping it, listening for messages, etc.

The main change in this PR is the unbundling of these two concepts into separate entities: LanguageRuntimeMetadata for the first and LanguageRuntimeSession for the second.

Language packs no longer need to provide both of these objects at once; now, it is only necessary to provide the metadata to tell Positron what runtimes are available.

When Positron is ready to start a session, it uses the metadata to ask the language pack to start a new session. The session object that is returned contains the controls for interacting with that specific session.

Services

Before this change, a single LanguageRuntimeService handled everything associated with runtimes. I have decomposed this service into three separate services in order to keep the original service from becoming too monolithic. Because services cannot have cyclic dependencies, the dependency graph is unidirectional:

graph TD
session[RuntimeSessionService] -- get registrations --> runtime[LangaugeRuntimeService] 
startup[RuntimeStartupService] -- get registrations --> 
runtime
startup -- autostart sessions --> session
Loading

LanguageRuntimeService

This service is a shell of its former self. All it does is provide a store of runtimes that are currently registered, and notify interested parties when that set changes. It could also be called the RuntimeRegistrationService.

RuntimeSessionService

This service provides facilities for maintaining the set of active runtime sessions of all kinds, including "console" sessions (the only kind understood prior to this PR) and "notebook" sessions (the kind introduced here).

Requests to start new sessions, query the set of active sessions, etc. all go through the RuntimeSessionService.

RuntimeStartupService

This service manages the tangled and asynchronous process that coordinates the startup of, and/or reconnection to, the correct runtime(s) for the current workspace. It attempts to organize this process into a sequence of well-defined phases.

Extension Model

The language packs have undergone significant changes to support this work. Most notably, the concept of a "provider" that creates a runtime from metadata is gone. In its place is a new concept: a RuntimeManager. It has three responsibilities:

  1. Discovery. The manager provides a way to discover the available runtimes on the machine
  2. Create a new session. The manager provides a way to create a new session for a given runtime.
  3. Reconnect to an existing session. The manager provides a way to rejoin an existing session, given the runtime and identifying information about the session to connect to.

These changes have enabled a number of improvements to the language packs. Moving a lot of code that use to run at startup time to session creation time has improved performance and fixed a number of bugs.

Session Startup Sequence

The session startup process typically works as follows:

  1. A request is received to start a new session from a given runtime.
  2. A unique ID is generated for the new session.
  3. Positron looks up the extension-supplied runtime manager that is associated with the runtime ID.
  4. Positron asks the extension's runtime manager to provision a new session.
  5. The language pack gets the new session ready to start (but does not start it!). It forms the Jupyter kernel arguments, creates methods, etc.
  6. The language pack returns a session object to Positron.
  7. Positron informs all interested parties that a new session has been created and is about to start. Anyone who's interested in the session can attach listeners before it begins starting up.
  8. Positron asks the new session to start.
  9. The startup sequence proceeds as it does today.

The most notable difference from how things work today is the provisioning step; it takes the place of the creation of the LanguageRuntime object, which used to occur at discovery time.

Debugging tools

image

Finally, this change also includes a debugging tool that lets you:

  • see a list of all active sessions, their mode, and their status
  • see detailed information about the runtime associated with each session
  • manually start/restart/etc. the session, or jump to its output log
  • see a list of active comms for the session along with a message counter for each (to watch traffic)
  • disconnect any comm

The tool is only intended for developers and fairly advanced debugging, so it's hidden by default. To turn it on, go here:

image

@softwarenerd

Copy link
Copy Markdown
Contributor

I built and ran the app. One thing I noticed is that after launching Positron in this state, with a Python file opened, a Python runtime was not started for me. Previously, a Python runtime was started.

image

@softwarenerd softwarenerd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The only real issue I found with this PR is that, for me anyway, it totally breaks R and Python.

Here are a couple of screen recordings:

Screen.Recording.2024-03-10.at.6.14.41.PM.mov
Screen.Recording.2024-03-10.at.6.14.41.PM.mov

I did ALL the things (including rebuild.sh).

I can't start R or Python. I can start Zed:

image

import { JupyterSerializedSession, workspaceStateKey } from './JupyterSessionSerialization';

/**
* LangaugeRuntimeAdapter wraps a JupyterKernel in a LanguageRuntime compatible interface.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: It's now LanguageRuntimeSessionAdapter.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, addressed in b87f0e5

Comment thread extensions/jupyter-adapter/src/utils.ts Resolved
* runtime.
*/
private async runtimeManagerForRuntime(metadata: ILanguageRuntimeMetadata): Promise<LanguageRuntimeManager | undefined> {
// Doe we already have a manager for this runtime? This happens when we

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: Doe

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Doe! b87f0e5

* @param sessionMode The mode in which the session is to be run.
* @param notebookUri The URI of the notebook document, if in notebook mode.
*
* Returns a Thenable that resolves with the ID of the newly created

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit comment: A Thenable that resolves to the new language runtime session instead of the ID.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed! b87f0e5

@softwarenerd softwarenerd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thunderbirds Are Go!

@jmcphers
jmcphers merged commit 2f20e42 into main Mar 12, 2024
@jmcphers
jmcphers deleted the feature/runtime-sessions branch March 12, 2024 18:00
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