Skip to content

clearing the history breaks nushell #7991

Closed
nushell/reedline
#536

Description

@amtoine

Describe the bug

when i add

  history: {
    file_format: "sqlite"
  }

to my config.nu, nushell creates ~/.config/nushell/history.sqlite3 to hold the history

however, running history --clear and then nu does not work and gives

Error:
  × error within history database: SqliteFailure(Error { code: SystemIoFailure, extended_code: 522 }, Some("disk I/
  │ O error"))

Note
this behaviour does not appear 100% of the time, but most of it

a quick fix is to remove the .sqlite3 files generated by history --clear

> ls ~/.config/nushell/history.sqlite3-* | get name
╭───┬───────────────────────────────────────────────────╮
│ 0 │ /home/amtoine/.config/nushell/history.sqlite3-shm │
│ 1 │ /home/amtoine/.config/nushell/history.sqlite3-wal │
╰───┴───────────────────────────────────────────────────╯

How to reproduce

  1. live inside nushell
  2. remove the history with history --clear
  3. try to open a new terminal or run nu from current session
  4. see the terminal crash or the error above

Expected behavior

i do not know what these history.sqlite3-shm and history.sqlite3-wal are here for, but they appear to block the startup of nushell...

maybe they shouldn't be here? 🤔

in the end i really would not want to have no shell nor any terminal...
if i'd close all my terminals after this first history --clear i would have had to drop into a TTY to fix nushell manually, that would have been definitely scary 😱

Screenshots

No response

Configuration

key value
version 0.75.0
branch
commit_hash 17a265b
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.66.1 (90743e729 2023-01-10)
rust_channel 1.66.1-x86_64-unknown-linux-gnu
cargo_version cargo 1.66.1 (ad779e08b 2023-01-10)
pkg_version 0.75.0
build_time 2023-02-04 13:06:35 +01:00
build_rust_channel release
features database, default, trash, which, zip
installed_plugins gstat, len, my_str_len, nu-example-1, nu-example-2, nu-example-3, pnet, query, query json, query web, query xml

Additional context

No response

Activity

  1. fdncred commented on Feb 6, 2023

    @fdncred
    Contributor

    I thought --clear was actually just removing the file itself. I wonder if the problem is that it's only removing the sqlite3 file but not removing all 3 sqlite files, i.e. the sqlite3 file, the wal file and the shm file.

  2. added
    good first issue
    A:line-editor
    category:bug
    and removed
    good first issue
    on Feb 6, 2023
  3. amtoine commented on Feb 7, 2023

    @amtoine
    MemberAuthor

    I thought --clear was actually just removing the file itself. I wonder if the problem is that it's only removing the sqlite3 file but not removing all 3 sqlite files, i.e. the sqlite3 file, the wal file and the shm file.

    looks this is exactly the issue 👍
    at least i can reproduce this behaviour each time it crashes 👌

  4. added 2 commits that reference this issue on Feb 8, 2023
    88f85ab
    12e9cf8
  5. rgwood commented on Feb 8, 2023

    @rgwood
    Contributor

    FYI I'm working on an improvement to reedline to fix this: nushell/reedline#536

  6. reopened this on Feb 9, 2023
  7. amtoine commented on Feb 9, 2023

    @amtoine
    MemberAuthor

    FYI I'm working on an improvement to reedline to fix this: nushell/reedline#536

    amazing 🤩

    is there a revision of nushell on which i can checkout and try to see if the bug still appears? 😌
    maybe the latest main on 16b99ed? not sure how the two interact, i think your change will need to be in [email protected] (as written in Cargo.toml) to take effect 🤔

  8. rgwood commented on Feb 9, 2023

    @rgwood
    Contributor

    is there a revision of nushell on which i can checkout and try to see if the bug still appears?

    Not yet. Need to wait for a new version of reedline to be published (I'm not sure how to do that; @sholderbach do you mind walking me through it sometime?) and then I'll submit a PR here to use the new Reedline functionality.

  9. 1 remaining item

  10. rgwood commented on Feb 9, 2023

    @rgwood
    Contributor

    Ah cool, I assumed we were pulling in published versions. Will take a look.

  11. reopened this on Feb 9, 2023
  12. rgwood commented on Feb 9, 2023

    @rgwood
    Contributor

    After fighting with error conversions for half an hour I thought I had it working, and... ugh:
    image

    Very weird that it would kill Nushell without a stack trace. I'm gonna clear my mind and come back to this another time 🧘

  13. fdncred commented on Feb 9, 2023

    @fdncred
    Contributor

    I was trying to help out here because I had an idea of trying to repro this in reedline. I wanted to see if reedline would puke too when clearing in repl mode. I was thinking something was pulling on it and not expecting 0 records.

    So, I added this to demo.rs around line 151.

                    if buffer.trim() == "clear-history" {
                        let hstry = Box::new(line_editor.history_mut());
                        hstry
                            .clear()
                            .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
                        continue;
                    }

    and I added this to engine.rs around like 414.

        /// Mutable view of the history
        pub fn history_mut(&mut self) -> &mut dyn History {
            &mut *self.history
        }

    It all compiles and works. :( I can't seem to get it to fail, even when writing to a real db.

    1. cargo r --example demo --features=sqlite
    2. history (and look at the entries we populated in there)
    3. clear-history (hope for a crash)
    4. Have a sad face because it works just fine. So, I'm guessing there must be something going on in nushell that's causing the weird behavior you're seeing.
  14. rgwood commented on Feb 9, 2023

    @rgwood
    Contributor

    Thanks for taking a look at that. This is my WIP: 7260a11

    Very strange that it crashes entirely. Maybe something in evaluate_repl()?

  15. fdncred commented on Feb 10, 2023

    @fdncred
    Contributor

    I've been reading a little bit about sqlite. I'm wondering if we need to do a checkpoint and vacuum at some interval or on exit. I also didn't realize the deleting doesn't delete, it just frees the pages.
    Here's the link I was reading in. I was mostly searching for a reason why the wal and shm don't remove themselves on exit.
    https://dzx.fr/blog/understanding-sqlite/#maintain-the-database

  16. rgwood commented on Feb 10, 2023

    @rgwood
    Contributor

    I'm wondering if we need to do a checkpoint and vacuum at some interval or on exit.

    Hmm, good question. I don't believe that's necessary for correctness, but it may be necessary to ensure that the deleted history is truly gone from disk (i.e. unrecoverable by looking through the WAL)

  17. fdncred commented on Feb 10, 2023

    @fdncred
    Contributor

    just documenting here for searching later. i used this command to cleanup my wal and shm files.

    1. ls
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a---           2/10/2023 11:19 AM        3399680 history.sqlite3
    -a---           2/10/2023  2:03 PM          32768 history.sqlite3-shm
    -a---           2/10/2023  2:00 PM        4214792 history.sqlite3-wal
    
    1. exit nushell
    2. sqlite3.exe -cmd 'VACUUM;' .\history.sqlite3
    3. ls
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a---           2/10/2023  2:10 PM        3174400 history.sqlite3
    
    1. start nushell in another window
    2. ls
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a---           2/10/2023  2:10 PM        3174400 history.sqlite3
    -a---           2/10/2023  2:14 PM          32768 history.sqlite3-shm
    -a---           2/10/2023  2:14 PM           4152 history.sqlite3-wal
    

    my wal and shm files are normal size now.

  18. rgwood commented on Feb 11, 2023

    @rgwood
    Contributor

    Used a debugger and confirmed that this is where Nu exits the REPL loop on my branch:

    .into_diagnostic()?; // todo: don't stop repl if error here?

    Still figuring out the best way to fix this. It's unfortunate that the Reedline struct contains a history_last_run_id field instead of delegating all history management to the History struct.

  19. NotTheDr01ds commented on Dec 6, 2024

    @NotTheDr01ds
    Contributor

    Given the challenges folks seem to have encountered trying to resolve this, is this still a good-first-issue?

  20. added
    A:database-SQL
    on Sep 9, 2025
  21. kronberger-droid commented on Nov 13, 2026

    @kronberger-droid
    Contributor

    Fixed by #19092.
    history --clear clears the sqlite history with SQL now and no longer deletes the database file, so the -wal and -shm files can't be left behind without it.
    I checked it on 0.117 with several sessions open, a new nu starts fine after clearing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A:database-SQLA:line-editorcategory:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions