Repository navigation
keep the console history when loadhistory() fails - #19688
Conversation
RStudio E2E - all platforms1 test(s) failed
All platforms - commit |
gtritchie
left a comment
There was a problem hiding this comment.
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> >( |
There was a problem hiding this comment.
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
Successwith 0 lines - GCC 16 (libstdc++):
failbitandbadbitset, so it returnsio_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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
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.
gtritchie
left a comment
There was a problem hiding this comment.
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.
# Conflicts: # src/cpp/session/modules/SessionRTests.cpp
…load-keeps-history
Takes #19688's loadFromFile(), with its staging buffer declared as a HistoryBuffer so it can swap with the history.
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.Rhistorywith 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 withverifyFileset, 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
verifyFileis set. Otherwise it reads into a separate buffer with the same capacity, swapping that in only once the read has succeeded. A missing file withverifyFileunset 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
verifyFilesays. libc++ opens a directory and reads it as an empty file, so on macOSloadhistory("~")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 theRloadhistorycallback on macOS and Linux, andrs_loadHistoryon Windows. The callers are unchanged. They already sent the client its history reset only after a successful load.The
ConsoleHistoryconstructor is now public so the test can use its own instance. The session's history is stillconsoleHistory().Automated Tests
New
SessionRTest.FailedHistoryLoadKeepsHistory(rsession gtests) seeds a separateConsoleHistory, so the session's own history and its listeners are left alone. It checks:verifyFileunset, a missing file still empties the history and succeeds.Against
main'sloadFromFile(), 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 reportsNo such file or directory,savehistory()still writes the earlier commands. Onmainit writes an empty file.QA Notes
loadhistory("no-such-file"). On macOS and Linux this reportsNo such file or directory. On Windows it reports nothing; savehistory() and loadhistory() fail without an error on Windows #19687 tracks that separately.savehistory(tmp <- tempfile()), thenreadLines(tmp). The commands from step 1 should be listed.Documentation
None needed.
Checklist
NEWS.md