Skip to content

source pane restores the wrong active tab after reload #17944

Closed

Description

@kevinushey

Summary

After a reload (browser refresh in server, or session restart), RStudio occasionally restores visibility of the wrong tab in the Source pane: the selected tab is not the one that was active before the reload.

Root cause analysis

The active tab is persisted across reloads as a plain integer index via client state (Source.java:538-559, the activeTab IntStateValue):

  • On save, getValue() returns columnManager_.getPhysicalTabIndex(), which goes through SourceColumn.getPhysicalTabIndex() (SourceColumn.java:303-311). That method maps the display's active tab index through the in-session tabOrder_ permutation (tabOrder_.indexOf(idx)).
  • On restore, onInit() calls columnManager_.initialSelect(value), which lands in SourceColumn.initialSelect() (SourceColumn.java:265-279) and passes the saved value directly to display_.selectTab(index) -- with no tabOrder_ mapping (the column is freshly built at that point, so tabOrder_ is the identity).

Meanwhile, the server returns documents at startup sorted by relativeOrder (falling back to creation time when relativeOrder == 0) in clientInitDocuments (SessionSource.cpp:1949, sortByRelativeOrder in SessionSourceDatabase.cpp:682-696). relativeOrder is only ever written by the set_doc_order RPC, which the client only calls from the drag-reorder handler onTabReorder (SourceColumn.java:1285).

So the saved integer and the restored document list are in different coordinate spaces, and the index points at the wrong document whenever the two diverge. Two concrete ways this happens:

Scenario A -- tabs were drag-reordered during the session

  1. Open documents A, B, C in order. editors_ = [A, B, C], tabOrder_ = [0, 1, 2].
  2. Drag C to the front. Physical order is now [C, A, B], tabOrder_ = [2, 0, 1], and set_doc_order persists relativeOrder C=1, A=2, B=3.
  3. Select tab A (physical index 1) and reload.
  4. On save: getPhysicalTabIndex() returns tabOrder_.indexOf(1) = 2.
  5. On restore: documents arrive in persisted order [C, A, B] and initialSelect(2) selects physical tab 2, which is B -- not A.

The in-session pair getPhysicalTabIndex() / setPhysicalTabIndex() are mutual inverses, so this permuted value works fine within a session; it is only initialSelect() on the restore path that consumes the value without the inverse mapping.

Scenario B -- a document was opened mid-session (no reordering needed)

New tabs are inserted immediately after the current tab, not at the end (SourceColumn.java:481-487), and this insertion does not call set_doc_order, so the new document keeps relativeOrder == 0.

  1. Open A, B, C in order (no reordering; all relativeOrder == 0).
  2. Select A, then open D. Physical order is now [A, D, B, C].
  3. With D active, reload. Saved index = 1.
  4. On restore, all documents have relativeOrder == 0, so the server sorts by creation time: [A, B, C, D]. initialSelect(1) selects B -- not D. (The visible tab order also differs from the pre-reload order, which is arguably a related bug.)

A hybrid of the two is also possible: once any reorder has happened, documents opened afterwards have relativeOrder == 0 and sort to the end on restore while having been displayed mid-list before it.

Additional fragility

  • With multiple source columns, a single integer is saved from the active column and replayed into whichever column is active after restore (SourceColumnManager.java:468-473).
  • Any change to the document set between the last client-state save and the restore (docs opened at session load by the server, closed tabs, etc.) shifts the index.

Suggested fix

Persist the active document's ID (or path) rather than an integer index, and resolve it to a tab after the documents are restored. Index-based persistence will keep breaking whenever the restored order differs from the in-session physical order. A smaller complementary fix is to call set_doc_order whenever a tab is inserted at a non-terminal position, so persisted order always matches physical order.

Metadata

Metadata

Assignees

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions