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: NonePriority: 5 - Normal
Status: DonePrivacy: Public
Assigned to: NoneOpen/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
  • -unavailable- added by chet (Updated the item)
  • -unavailable- added by balacobaco (Voted in favor of this item)
  • -unavailable- added by rstuart (Submitted the item)
  •  

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

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 4 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Fri 05 Jun 2015 06:07:53 PM UTCchetStatusNone=>Done
    Wed 03 Jun 2015 12:44:59 AM UTCbalacobacoCarbon-Copy-=>Added balacobaco
    Fri 29 May 2015 03:45:55 AM UTCrstuartAttached File-=>Added safe-history-write.patch, #34121
    Thu 28 May 2015 08:28:11 AM UTCrstuartAttached File-=>Added safe-history-write.patch, #34104

    Back to the top


    Powered by Savane 3.1-cleanup1