patchThe 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

Submitted by:  Russell Stuart <rstuart>
Submitted on:  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

Thu 28 May 2015 08:28:11 AM UTC, original submission:

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.

Russell Stuart <rstuart>

 

Attached Files
file #34121:  safe-history-write.patch added by rstuart (4KiB - text/x-patch - Eliminate another race condition)
file #34104:  safe-history-write.patch added by rstuart (4KiB - text/x-patch)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Updated the item)
  • -email is unavailable- added by balacobaco (Voted in favor of this item)
  • -email is unavailable- added by rstuart (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can add your encouragement to it.
    This task has 10 encouragements so far.

    Only logged-in users can vote.

     

     

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2015-06-05 chet StatusNone => 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

    Back to the top


    Powered by Savane 3.3