Skip to content

SourceColumn.onSelection can dereference a null activeEditor_ during re-entrant tab activation #18736

Closed

Description

@gtritchie

System details

RStudio Edition : Desktop
RStudio Version : 2026.09.0+174 (rel-autumn-hawkbit, 870df5ed78)
OS Version      : macOS 14 arm64 (GitHub Actions runner)
R Version       : 4.1.3

Seen in CI rather than through interactive use, in the certification e2e run
https://github.com/rstudio/rstudio/actions/runs/34260015710 (cell
macOS 14 arm64 / e2e-desktop-macos14 (2)). It surfaced as an uncaught client
exception during tests/panes/files/files_date_format.test.ts, which is
incidental -- that spec changes a preference and the Source column reacts. This
is not a Files-pane or date-format bug.

The test passed on retry, and the same spec passed on the other nine cells.

Steps to reproduce the problem

No deterministic reproducer yet. The captured stack is unambiguous about how the
throwing frame was reached (symbols resolved with the GWT symbolMap from an
ant build at the same commit, and independently corroborated by reading the
shipped 663CCD2A66B850C6CC3EDA8577CAC600.cache.js):

TextEditingTargetWidget$7.run          (TextEditingTargetWidget.java:993)  document-outline latch Timer
  SourceColumnManager.setActive        (SourceColumnManager.java:516)
    SourceColumn.setActiveEditor       (SourceColumn.java:362)
      SourceColumn.onActivate          (SourceColumn.java:343)
        SourcePane.selectTab           (SourcePane.java:153)
          TabLayoutPanel.selectTab     (:711)
            DocTabLayoutPanel.selectTab
              HandlerManager.fireEvent / SimpleEventBus.doFire
                SelectionEvent.dispatch
                  SourceColumn.onSelection  (SourceColumn.java:1188)  <-- throws

Reported to the client as:

Exception caught: (TypeError) : Cannot read properties of null (reading 'NN')

where NN is EditingTarget.getPath().

Describe the problem in detail

SourceColumn.onSelection assigns activeEditor_, makes two calls, and then
dereferences the field again:

activeEditor_ = editors_.get(event.getSelectedItem());   // 1197
activeEditor_.onActivate();                              // 1198
manager_.setActive(name_);                               // 1199

// let any listeners know this tab was activated
events_.fireEvent(new DocTabActivatedEvent(              // 1202
      activeEditor_.getPath(),                           // 1203  <-- NPE
      activeEditor_.getId()));

activeEditor_ cannot have been null at 1198 -- onActivate() would have
thrown first -- so it was nulled between 1198 and 1203, i.e. by something one
of those two calls re-entered. Three statements in the class null the field:

  • SourceColumn.onSelection:1193 -- a re-entrant selection whose
    getSelectedItem() < 0 would leave it null on return.
  • SourceColumn.setActiveEditor(String):376 -- nulls it when handed an empty
    docId. SourceColumnManager.setActive(SourceColumn):526 calls
    prevColumn.setActiveEditor("") on exactly that path.
  • SourceColumn.removeTab (around :1397), on the close path.

The stack does not name which one, because the culprit frame had already
returned by the time line 1203 ran. That is the one open question here.

Re-entrancy is structurally easy to reach: SourceColumn.onActivate:358 calls
display_.selectTab(...), which fires a selection event synchronously, and
onSelection in turn calls back into SourceColumnManager.setActive. The
field is live across all of it.

How likely is this in practice

The trigger is common but the window is narrow.

  • The entry point is the new Timer() in TextEditingTargetWidget (:989-:993,
    commented "Time-out setting the latch just to ensure the document outline has
    actually been appropriately rendered"), which runs on source-document widget
    construction. Opening a document is among the most frequent things a user
    does.
  • Landing in the fault needs that timer to fire while a column/tab selection is
    mid-flight and something nulls activeEditor_ inside a two-statement window.
    Being timer-driven, it is load-dependent -- it showed up on the slowest,
    most network-degraded cell in the run and nowhere else.
  • Multiple source columns make the prevColumn != activeColumn_ branch (and so
    the setActiveEditor("") null) reachable in normal use.

So: rare per action, attached to very frequent actions, and more likely on
loaded machines. Expect it at fleet scale rather than in a given session.

Consequences

The throw happens inside event dispatch, so it is caught by GWT's uncaught
handler and does not crash the IDE. The cost is that DocTabActivatedEvent is
never fired for that activation, and the remainder of onSelection -- including
the deferred focus block after :1203 -- never runs. Its three consumers:

  • SourceColumnManager:297 -> setActiveDocId(event.getId()). The manager's
    notion of the active document goes stale.
  • SourceWindowManager:874 -> server_.chatDocFocused(event.getId(), ...),
    which exists (per its own comment) so Posit Assistant's insertAtCursor
    works when documents are activated without getting focus. Missing it means
    the Assistant can target the wrong document.
  • DocsMenu:57 -> refreshStyles(event.getId()). The Docs menu keeps stale
    active-document styling.

The user-visible shape is therefore a tab that activates without taking
keyboard focus, a stale Docs menu, and -- the one with real blast radius -- an
Assistant insertion that can land in the wrong file.

Suggested direction

Hold the editor in a local and use it for the event, so a re-entrant call
cannot pull it out from under the rest of the method:

EditingTarget editor = editors_.get(event.getSelectedItem());
activeEditor_ = editor;
editor.onActivate();
manager_.setActive(name_);
events_.fireEvent(new DocTabActivatedEvent(editor.getPath(), editor.getId()));

That removes the crash. Whether the re-entrancy itself should be prevented, and
which of the three nulling statements is firing, is worth settling first --
answering it may show the same pattern in the sibling reads of activeEditor_
in this class.

Diagnosability note

Resolving this needed a local ant build at the matching commit to get a
GWT symbolMap, because obfuscated CI stacks are otherwise unreadable.
src/gwt/build.xml:371 already writes symbolMaps to src/gwt/extras, and
:557 deletes them on clean. Publishing that directory as a build artifact
would make future obfuscated stacks resolvable without reproducing the build.

Activity

  1. added theissue type on Sep 8, 2026
  2. gtritchie commented on Sep 8, 2026

    @gtritchie
    MemberAuthor

    Not a regression — the crash is ~5 years old, but its blast radius is new

    Worth recording so nobody triages this as a recent regression. Dates are from
    git blame/git log on this checkout.

    Date Change Effect
    2010-12-07 activeEditor_ = null + the getSelectedItem() >= 0 guard (initial-import boundary) shape exists
    2015-06-10 doc-outline latch Timer in TextEditingTargetWidget the trigger
    2015-12-09 fireEvent(new DocTabActivatedEvent(activeEditor_.getPath(), ...)) the deref
    2020-04-21 activeEditor_.onActivate() inserted between the assignment and the deref (6e59b22c153) window opens
    2020-06-24 manager_.setActive(name_) added inside the window (eff0599412d) window widens
    2020-09-29 prevColumn.setActiveEditor("") in setActive(SourceColumn) (0ebfc95323f) concrete nulling path
    2025-12-16 SourceWindowManager.onDocTabActivated -> chatDocFocused (aedee17618b) blast radius grows

    Before 2020-04-21 the field was assigned and dereferenced with no call-out in
    between, so there was no re-entrancy window at all. Everything that opened and
    widened it is the multi-column Source work -- c363e9f673c and 97eddbe8a25
    ("pull logic from Source to create a SourceColumn class and SourceColumnManager
    class...") -- which shipped in RStudio 1.4. The code moved
    Source.java -> SourcePane.java -> SourceColumn.java across that refactor,
    which is why blame on the current file only reaches 2020.

    The genuinely new piece is the Assistant handler, added 2025-12-16. Before it, a
    missed DocTabActivatedEvent cost a stale Docs menu and a stale activeDocId.
    Now it also means Posit Assistant is not told which document is focused, so
    insertAtCursor can target the wrong file. In other words: old crash, new
    consequence.

    Two limits on this analysis:

    • It establishes when the code became vulnerable, not how often it has fired. No
      crash telemetry was consulted, so it may have been happening rarely for years.
    • The likeliest answer to "why are we seeing it now" is detection, not behavior.
      The e2e uncaught-client-exception capture landed 2026-06-12; an exception like
      this is swallowed by GWT's uncaught handler and would not have failed a test
      before that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions