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

Improve restarting behavior of the reticulate session. #5019

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions extensions/positron-reticulate/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,20 +484,45 @@ class ReticulateRuntimeSession implements positron.LanguageRuntimeSession {
// tied to the R session.
// We have to send a restart to the R session, and send a reticulate::repl_python()
// command to it.
this.pythonSession.shutdown(positron.RuntimeExitReason.Restart);
await this.rSession.restart();
const rSession = await getRSession();
rSession.execute(
'reticulate::repl_python()',
'start-reticulate',
positron.RuntimeCodeExecutionMode.Interactive,
positron.RuntimeErrorBehavior.Continue
const restart = await positron.window.showSimpleModalDialogPrompt(
vscode.l10n.t('Restarting reticulate'),
vscode.l10n.t('This is will also restart the parent R session. Are you sure you want to continue?'),
vscode.l10n.t('Yes'),
vscode.l10n.t('No')
);

if (!restart) {
throw new Error('Restart cancelled.');
}

// The events below will make sure that things occur in the right order:
// 1. shutdown the current reticulate session
// 2. restart the attached R session
// 3. start a new reticulate session.
this.pythonSession.onDidEndSession((sess) => {
this.rSession.restart();
});

const disposeListener = this.rSession.onDidChangeRuntimeState(async (e) => {
if (e === positron.RuntimeState.Ready) {
this.rSession.execute(
'reticulate::repl_python()',
'start-reticulate',
positron.RuntimeCodeExecutionMode.Interactive,
positron.RuntimeErrorBehavior.Continue
);
// This should only happen once, so we dispose of this event as soon
// as we have started reticulate.
disposeListener.dispose();
}
});

await this.shutdown(positron.RuntimeExitReason.Restart);
return;
}

public async shutdown() {
await this.pythonSession.shutdown(positron.RuntimeExitReason.Shutdown);
public async shutdown(exitReason: positron.RuntimeExitReason) {
await this.pythonSession.shutdown(exitReason);
// Tell Positron that the kernel has exit. When launching IPykernel from a standalone
// process, when the kernel exits, then all of it's threads, specially the IOPub thread
// holding the ZeroMQ sockets will cease to exist, and thus Positron identifies that the
Expand Down