Skip to content

keep the console history when loadhistory() fails - #19688

Merged
kevinushey merged 7 commits into
mainfrom
bugfix/19686-history-load-keeps-history
Nov 19, 2026
Merged

kevinushey merged 7 commits into
mainfrom
bugfix/19686-history-load-keeps-history

Conversation

@kevinushey

@kevinushey kevinushey commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Intent

A loadhistory() call that failed, for example because of a mistyped path, emptied the session's console history. The Console's command list looked unchanged, but the session's copy is what gets saved. With "Always save history" on (the default), quitting then replaced .Rhistory with only the commands run after the failed call. The emptied history also carried across a suspend and resume.

Fixes #19686.

Approach

ConsoleHistory::loadFromFile() cleared the history first, then checked whether the file existed and read straight into the history. Any failure (a missing file with verifyFile set, an unreadable file, a read error part-way through) left the history empty or partial.

It now returns the error without touching the history when the file is missing and verifyFile is set. Otherwise it reads into a separate buffer with the same capacity, swapping that in only once the read has succeeded. A missing file with verifyFile unset still clears the history and succeeds, as before. That path is used at session startup and resume, which load into a fresh, empty history.

A path that is a directory fails too, whatever verifyFile says. libc++ opens a directory and reads it as an empty file, so on macOS loadhistory("~") used to replace the history with nothing and report success.

One gap remains on macOS: libc++ reports a read error part-way through the file as end of file, so that load succeeds with the lines read so far. readCollectionFromStream() can't tell the difference from the stream. libstdc++ (Linux) reports it as an error.

loadhistory() reaches this function on every platform: through the Rloadhistory callback on macOS and Linux, and rs_loadHistory on Windows. The callers are unchanged. They already sent the client its history reset only after a successful load.

The ConsoleHistory constructor is now public so the test can use its own instance. The session's history is still consoleHistory().

Automated Tests

New SessionRTest.FailedHistoryLoadKeepsHistory (rsession gtests) seeds a separate ConsoleHistory, so the session's own history and its listeners are left alone. It checks:

  • A load from a missing file fails, and the seeded commands are kept in order.
  • On POSIX, a load from an existing file with mode 000 fails, and the seeded commands are kept. This check only runs when the file is really unreadable, since root can read it.
  • A load from a directory fails, and the seeded commands are kept.
  • A load from a readable file replaces the history with that file's commands, in order.
  • With verifyFile unset, a missing file still empties the history and succeeds.

Against main's loadFromFile(), the missing-file, unreadable-file, and (on macOS) directory checks all fail, with the history coming back empty. The full rsession gtest suite passes locally (887 tests, macOS, R 4.6.1).

Also checked end to end in a macOS rsession: after loadhistory() of a missing file reports No such file or directory, savehistory() still writes the earlier commands. On main it writes an empty file.

QA Notes

  1. Run a few commands in the Console.
  2. Run loadhistory("no-such-file"). On macOS and Linux this reports No such file or directory. On Windows it reports nothing; savehistory() and loadhistory() fail without an error on Windows #19687 tracks that separately.
  3. Run savehistory(tmp <- tempfile()), then readLines(tmp). The commands from step 1 should be listed.

Documentation

None needed.

Checklist

  • If this PR adds a new feature, or fixes a bug in a previously released version, it includes an entry in NEWS.md
  • If this PR adds or changes UI, the updated UI meets accessibility standards (n/a: no UI)
  • A reviewer is assigned to this PR (if unsure who to assign, check Area Owners list)
  • This PR passes all local unit tests

@kevinushey kevinushey added this to the 2026.12 (Parrot Balsam) milestone Nov 18, 2026
@kevinushey
kevinushey requested a review from gtritchie November 18, 2026 06:04
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

1 test(s) failed

###################- 99% pass rate

Passed Failed Skipped Flaky
5759 1 254 1
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 4 0 16 0
✅ desktop-linux 1441 0 49 0
❌ desktop-macos 1447 1 43 0
✅ desktop-windows 1436 0 60 0
✅ server-linux 1383 0 54 1
✅ setup 48 0 32 0

View report online


All platforms - commit 8816015deceff4d575da665f40b4768313c5a20f - Run #2397

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The fix works for missing and unreadable files, and the test covers both. On macOS, though, a load from a directory still empties the history; details inline, along with two smaller points.

Requesting changes for the macOS directory case. The other two are minor.

}

boost::circular_buffer<std::string> loaded(historyBuffer_.capacity());
Error error = core::readCollectionFromFile<boost::circular_buffer<std::string> >(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On macOS, loading from a path that exists but is a directory, e.g. loadhistory("~"), still replaces the history with an empty one, and reports success.

exists() is true for a directory, and libc++'s std::ifstream opens it. The first getline() comes back empty with eofbit and failbit set, so readCollectionFromStream() takes the is.eof() branch (FileSerializer.hpp:218), breaks, and returns Success() with nothing read. loaded is empty, it gets swapped in, and Rloadhistory() calls consoleHistoryReset().

I checked with a standalone copy of that read loop, opening /tmp:

  • Apple clang (libc++): returns Success with 0 lines
  • GCC 16 (libstdc++): failbit and badbit set, so it returns io_error

So Linux builds report an error. I'd expect Windows to as well, since CreateFileW without FILE_FLAG_BACKUP_SEMANTICS won't open a directory, but I haven't tried it. main behaves the same way, so this isn't a regression, but it's the same data loss this PR fixes. Returning an error when filePath.isDirectory() is true would cover it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Confirmed: under Apple clang, a std::ifstream on /tmp opens, and the first getline() comes back empty with eofbit|failbit. Fixed in 3cbed38: loadFromFile() now returns is_a_directory for a directory before reading, whatever verifyFile says, so loadhistory("~") reports "Is a directory" and keeps the history. Linux already returned io_error on that path, so a .Rhistory directory at startup now reports an error on macOS too.

ERROR_LOCATION);
}
else
// a failed load keeps the current history; it would otherwise be what the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This comment, and "a read error part-way through" in the PR description, may not hold on macOS. libc++'s basic_filebuf::underflow() calls fread() and doesn't check ferror(), so an I/O error part-way through the file looks like end of file. readCollectionFromStream() then returns Success() with the lines read so far, and those replace the history. libstdc++ sets badbit on a failed read, so Linux returns io_error.

I couldn't trigger a real mid-file read error, so this is from reading the libc++ <fstream> header, not from a reproduction. If it holds, the gap is in readCollectionFromStream() rather than here. Checking is.bad() wouldn't catch it either, since libc++ never sets it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, it holds: libc++'s basic_filebuf::underflow() treats fread() returning 0 as end of file and never checks ferror(). The directory case above goes through the same code. I've corrected the PR description. I'd keep the fix out of this PR: readCollectionFromStream() only sees an istream, and with libc++ nothing on the stream shows the error, so catching it means changing how FilePath::openForRead() reads on POSIX, which every readCollectionFromFile() caller shares. I left this comment as is, since it describes what loadFromFile() does with an error it gets back.

EXPECT_EQ("", value);
}

TEST(SessionRTest, FailedHistoryLoadKeepsHistory) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could this also cover a directory? loadFromFile() on a temp directory with verifyFile set should fail and keep seeded. On macOS that check fails today (see the comment on RConsoleHistory.cpp).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Added in 3cbed38. Without the directory check, the new assertions fail on macOS: the load returns success and the history comes back {}.

typedef RSTUDIO_BOOST_SIGNAL<void (const std::string&, bool)> AddSignal;

private:
// the session's history is consoleHistory(); tests use their own instance

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Line 38 still marks ConsoleHistory as // singleton. With the constructor public that's no longer true; could it say consoleHistory() returns the session's history, like this comment does?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in d8405c7.

libc++ opens a directory and reads it as an empty file, so on macOS loadhistory("~") replaced the history with nothing and reported success.

Addresses #19686.
@kevinushey
kevinushey requested a review from gtritchie November 19, 2026 17:10

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The directory check and its test look good, and the macOS read-error gap is now documented in the description. The Jenkins build-package failure on this head looks unrelated (it failed after 2 minutes, and both changed files compile cleanly here), but it needs a rerun before merging.

@kevinushey
kevinushey merged commit 811004c into main Nov 19, 2026
1 of 4 checks passed
@kevinushey
kevinushey deleted the bugfix/19686-history-load-keeps-history branch November 19, 2026 20:00
kevinushey added a commit that referenced this pull request Nov 19, 2026
Takes #19688's loadFromFile(), with its staging buffer declared as a
HistoryBuffer so it can swap with the history.
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.

a failed loadhistory() discards the session's console history

2 participants