The GNU Bourne-Again SHell - Patches: patch #8676, Fix truncating .bash_history
You are not allowed to post comments on this tracker with your current authentication level.
patch #8676: Fix truncating .bash_history
Submitter: | Russell Stuart <rstuart> | ||
Submitted: | Thu 28 May 2015 08:28:11 AM UTC | ||
Votes: | 10 | ||
Category: | None | Priority: | 5 - Normal |
Status: | Done | Privacy: | Public |
Assigned to: | None | Open/Closed: | Open |
Attached Files
file #34121: safe-history-write.patch added by rstuart (4KiB - text/x-patch - Eliminate another race condition)
Depends on the following items: None found
Items that depend on this one: None found
Carbon-Copy List
There are 10 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.
Follow 4 latest changes.
Date | Changed by | Updated Field | Previous Value | => | Replaced by |
---|---|---|---|---|---|
2015-06-05 | chet | Status | None | ![]() |
Done |
2015-06-03 | balacobaco | Carbon-Copy | - | ![]() |
Added balacobaco |
2015-05-29 | rstuart | Attached File | - | ![]() |
Added safe-history-write.patch, #34121 |
2015-05-28 | rstuart | Attached File | - | ![]() |
Added safe-history-write.patch, #34104 |
Bash loses all my history on occasion. This will eventually happen to everyone who runs multiple bash sessions, eg:
http://superuser.com/questions/20900/bash-history-loss
The problem occurs when multiple instances exit simultaneously. One example is when you close a multi tab xterm.
It happens when bash is enforcing HISTFILESIZE. Bash re-reads the history, then truncates the file, then the smaller fine. If several bash instances are doing this as the same time there is a race condition. It's possible for one bash instance to read .bash_history between when it is truncated and when it is written, and thus it sees a zero length file. If the last version of .bash_history is this 0 length version all history is lost.
The attached patch works around the problem by writing the new version to a temporary file, then renaming it. Since POSIX guarantees rename() is atomic the race is gone.