Skip to content

file.edit() can silently fail when invoked during an in-flight closeAllSourceDocs #17738

Closed

Description

@kevinushey

Symptom

file.edit() (and other "open in Source pane" events) can silently fail to open a tab when invoked shortly after closeAllSourceDocs. No new tab appears, no error surfaces in the console -- the open call is effectively dropped.

Reproducer

The race is reliable when an R-side file.edit() is fired with no wait after a UI-level "close all docs" command. Concretely, this Playwright sequence reproduces ~100% of the time on macOS desktop:

await executeCommand(page, 'closeAllSourceDocs');
// (no wait)
await executeInConsole(page, 'file.create("foo.R")');
await executeInConsole(page, 'file.edit("foo.R")');
// 20s timeout waiting for the foo.R tab to appear -- never does.
await expect(sourcePane.selectedTab).toContainText('foo.R', { timeout: 20000 });

Originally observed in e2e/rstudio/tests/panes/editor/rmarkdown.test.ts when the previous test's cleanup called closeAllSourceDocs and the next test's file.edit() raced it.

Handler chain

file.edit from R -> .rs.registerReplaceHook (src/cpp/session/modules/SessionSource.R:323) -> rs_fileEdit (src/cpp/session/modules/SessionSource.cpp:1791) -> enqueFileEditEvent -> GWT Source.onFileEdit (src/gwt/src/org/rstudio/studio/client/workbench/views/source/Source.java:2376) -> fileTypeRegistry_.editFile -> ... -> OpenSourceFileEvent -> Source.onOpenSourceFile -> doOpenSourceFile.

The close pipeline is Source.closeAllSourceDocs -> SourceColumnManager.closeAllLocalSourceDocs -> closeAllTabs -> cpsExecuteForEachEditor, which uses a SerializedCommandQueue and serializes close commands across asynchronous boundaries. cpsExecuteForEachEditor clones the editor list before iterating (SourceColumnManager.java:2949), so a newly opened tab is not directly closed by the loop -- yet the new tab still doesn't end up visible. The drop appears to happen somewhere across the multi-hop GWT event-bus dispatch in the open chain while the close pipeline is draining.

Affected handlers

Same race likely applies to every "open content in the Source pane" handler in Source.java:

  • onFileEdit (Source.java:2376) -- confirmed
  • onOpenSourceFile (Source.java:1984) -- shares doOpenSourceFile with above
  • onOpenPresentationSourceFile (Source.java:1996)
  • onEditPresentationSource (Source.java:2011)
  • onEditPresentation2Source (Source.java:2068)
  • onXRefNavigation (Source.java:2090)
  • onShowContent (Source.java:818)
  • onOpenObjectExplorerEvent (Source.java:843)
  • onShowData (Source.java:873)
  • onShowProfiler (Source.java:896)
  • onNewDocumentWithCode (Source.java:1898)

Proposed fix

Gate the open-content handlers on a closeAllInProgress_ flag set by closeAllSourceDocs. Open events that arrive during a close go onto a pendingOpenCommands_ queue and run when the close's onCompleted fires. Sketch:

private boolean closeAllInProgress_;
private final ArrayList<Command> pendingOpenCommands_ = new ArrayList<>();

public void closeAllSourceDocs(String caption, Command onCompleted, String excludeDocId) {
    closeAllInProgress_ = true;
    Command wrapped = () -> {
        closeAllInProgress_ = false;
        ArrayList<Command> drained = new ArrayList<>(pendingOpenCommands_);
        pendingOpenCommands_.clear();
        for (Command c : drained) c.execute();
        if (onCompleted != null) onCompleted.execute();
    };
    // existing logic, passing `wrapped` instead of `onCompleted`
}

private void runOrDeferUntilCloseComplete(Command cmd) {
    if (closeAllInProgress_) pendingOpenCommands_.add(cmd);
    else cmd.execute();
}

public void onFileEdit(FileEditEvent event) {
    if (!SourceWindowManager.isMainSourceWindow()) return;
    runOrDeferUntilCloseComplete(() -> {
        FileSystemItem file = event.getFile();
        file.setFocusOnNavigate(true);
        fileTypeRegistry_.editFile(file, event.getFilePosition());
    });
}
// ...same wrapping for the other handlers listed above

Current workaround

Test-side, closeSourceAndDeleteFile in e2e/rstudio/actions/source_pane.actions.ts polls for selectedTab.toHaveCount(0) (in addition to aceTextInput.toHaveCount(0)) before issuing the next file.edit. This eliminates the race in tests but is a workaround, not a fix -- any real user driving the IDE programmatically (R scripts, RStudio API consumers, automation tooling) hits the same race.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugnew

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions