Skip to content

Keep map of RRuntime objects in extension, use in r.packageInstall command - #1292

Merged
juliasilge merged 5 commits into
mainfrom
install-restart-runtime-in-ext
Sep 12, 2023
Merged

juliasilge merged 5 commits into
mainfrom
install-restart-runtime-in-ext

Conversation

@juliasilge

Copy link
Copy Markdown
Member

Addresses #1091 by ripping out the "watch for DESCRIPTION file change" piece and replace it with keeping a map of RRuntime objects in the R extension that we can execute and watch for events with. This is basically like this option I outlined in #1279:

If we could check the status of an R runtime (busy/idle), we could find out when devtools::install() was no longer running.

However, this probably isn't a final version because we would like to know if the installation failed or not, to early return and not restart and library(package_name).

@juliasilge juliasilge left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I do think eventually we will want to rip this out again and wait for executeCode() to return something we can parse. Given how much variety there is in R .libPaths() (which is more than I even expected TBH) I now believe this, acting at the RRuntime level, is a decent option for now.

Comment thread extensions/positron-r/src/commands.ts Outdated
const runtime = runtimes.get(runningRuntimes[0].runtimeId);
if (runtime) {
runtime.execute('devtools::install()',
randomUUID(),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jmcphers you mentioned something today about using this ID to make sure we are waiting for the right code to finish, but this execute is synchronous so I think we're OK like this. Let me know if I am misunderstanding!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unfortunately this execute is not synchronous. It might appear so because the restart command can't make any progress until the execute command finishes, but we probably shouldn't bet on it because there's no guarantee the restart won't take priority (now or in the future).

What I'd suggest is:

  • first generate the UUID
  • then establish a listener to language runtime messages; it should listen for an idle LanguageRuntimeState message whose parent ID is the UUID
  • then perform the execution request
  • then wait for the idle message to arrive

When an idle message arrives whose parent ID is the UUID you gave to execute(), you'll know the code is done running.

Comment on lines +60 to +61
if (runtimeState === positron.RuntimeState.Starting) {
await delay(500);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

After playing around with this, I think this works the best and will be the least likely to end up in a bad situation:

  • Kick off the restart
  • Start a listener that waits for Starting, then waits a smidge more, then executes the second piece of code

@juliasilge
juliasilge marked this pull request as ready for review September 11, 2023 23:32
@juliasilge
juliasilge requested a review from jmcphers September 11, 2023 23:32
Comment thread extensions/positron-r/src/commands.ts Outdated
const runtime = runtimes.get(runningRuntimes[0].runtimeId);
if (runtime) {
runtime.execute('devtools::install()',
randomUUID(),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unfortunately this execute is not synchronous. It might appear so because the restart command can't make any progress until the execute command finishes, but we probably shouldn't bet on it because there's no guarantee the restart won't take priority (now or in the future).

What I'd suggest is:

  • first generate the UUID
  • then establish a listener to language runtime messages; it should listen for an idle LanguageRuntimeState message whose parent ID is the UUID
  • then perform the execution request
  • then wait for the idle message to arrive

When an idle message arrives whose parent ID is the UUID you gave to execute(), you'll know the code is done running.

runtime.onDidReceiveRuntimeMessage(runtimeMessage => {
if (runtimeMessage.parent_id === id &&
runtimeMessage.type === positron.LanguageRuntimeMessageType.State) {
const runtimeMessageState = runtimeMessage as positron.LanguageRuntimeState;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is this the right way to do this? i.e. get from a plain message to a message that is specifically about state as positron.LanguageRuntimeState?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yep, that's the right idea! Make sure that the event handler is unregistered after you get a match.

@juliasilge
juliasilge requested a review from jmcphers September 12, 2023 17:22

@jmcphers jmcphers left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, I think this'll work until we get a better API for awaitable execution!

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