Skip to content

Commit

Permalink
Improve restarting behavior of the reticulate session. (#5019)
Browse files Browse the repository at this point in the history
Addresses #4887 by making
sure the correct order of action happens:

1. Shutdown the Reticulate Python kernel
2. Restart the R session
3. Start a new reticulate session

---------

Signed-off-by: Daniel Falbel <dfalbel@gmail.com>
Co-authored-by: Jonathan <jonathan@rstudio.com>
  • Loading branch information
dfalbel and jmcphers authored Oct 16, 2024
1 parent 25b4e69 commit b17da0a
Showing 1 changed file with 35 additions and 10 deletions.
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.Shutdown);
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

0 comments on commit b17da0a

Please sign in to comment.