Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save history when the shell is exiting abnormally #2657

Closed
dratini0 opened this issue May 28, 2018 · 5 comments · Fixed by #5418 or #5425
Closed

Save history when the shell is exiting abnormally #2657

dratini0 opened this issue May 28, 2018 · 5 comments · Fixed by #5418 or #5425

Comments

@dratini0
Copy link

dratini0 commented May 28, 2018

When using the JSON backend, the history is not saved unless the shell is exited properly (for example, by ^D). When the exec alias is used, or a crash is induced (by for example adding invalid colors into $BOTTOM_TOOLBAR), the history file is not saved, and is left locked without commands being written into it.

My suspicion is that this is due to the fact that exit handlers are not ran when exec is used. This doesn't happen with SQLite for obvious reasons.

For community

⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

@dratini0
Copy link
Author

For reference, xonfig says:

+------------------+-----------------+
| xonsh            | 0.6.3           |
| Python           | 3.6.5           |
| PLY              | 3.11            |
| have readline    | True            |
| prompt toolkit   | 1.0.15          |
| shell type       | prompt_toolkit  |
| pygments         | 2.2.0           |
| on posix         | True            |
| on linux         | True            |
| distro           | debian          |
| on darwin        | False           |
| on windows       | False           |
| on cygwin        | False           |
| on msys2         | False           |
| is superuser     | False           |
| default encoding | utf-8           |
| xonsh encoding   | utf-8           |
| encoding errors  | surrogateescape |
+------------------+-----------------+

@gforsyth
Copy link
Collaborator

Thanks for reporting @dratini0! I can confirm this behavior.

@scopatz
Copy link
Member

scopatz commented Jun 13, 2018

My suspicion is that this is due to the fact that exit handlers are not ran when exec is used.

Really? I thought that atexit was always run...

@ofek
Copy link

ofek commented Jun 19, 2022

This still is bugged fyi

@ofek
Copy link

ofek commented Jun 19, 2022

For others wishing to move to SQLite, use this once:

How to convert history to sqlite?

from xonsh.history.json import JsonHistory
from xonsh.history.sqlite import SqliteHistory

class History(SqliteHistory):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.clear()

        jh = JsonHistory(**kwargs)
        for item in jh.all_items():
            self.append(item)

@anki-code anki-code removed the bug label May 2, 2024
@anki-code anki-code changed the title History is not always saved when using the JSON backend Save history when the shell is exiting abnormally May 15, 2024
jnoortheen pushed a commit that referenced this issue May 16, 2024
)

### Before

```xsh
echo 1 
echo 2
# <<Crash>>
# No json history
```

### After

Closes #2657

```xsh
echo 1 
echo 2
# <<Crash>>
# In json history
# echo 1
# echo 2
```

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
gforsyth pushed a commit that referenced this issue May 22, 2024
### Motivation

Development tools like PyCharm send SIGINT before SIGKILL in case of
reload app. Saving history on SIGINT is the last chance to save history
in this case.

I understand that history will be flushed on Ctrl+C (SIGINT) but it
looks like no bad consequences of this. In addition history saving will
be more stable.

### Before
```xsh
# Development in PyCharm
xonsh --no-rc
echo cmd
# Ctrl+RR of Ctrl+DD to restart.
# Command history lost.
```

### After
```xsh
# Development in PyCharm
xonsh --no-rc
echo cmd
# Ctrl+R Ctrl+R for restart
# Command history is here.
```

Closes #2657

### Env

Use `$XONSH_HISTORY_SIGINT_FLUSH=False` to switch it off.

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants