On 7/19/13 3:40 AM, Geoff Kuenning wrote: > Now, to clarify: the difficulty isn't that bash overwrites the history > file. That's the default behavior, and it's to be expected. If a user > opens three shells (in any fashion) and then successively types "exit" > in each, it's to be expected that only the last one's history would be > written to HISTFILE. And that's what I, personally, want to happen. > > But right now, if all three of those shells exit simultaneously--for > whatever reason--there is a significant probability that the history > file will end up zero-length. That's not theoretical; I've experienced > it multiple times. And that's a bug, plain and simple. And I suspect > that it can be fixed on 99+% of all deployed systems by just adding > O_EXCL to the open.
Let's think about why and how this could happen. Maybe that will give some insight into how to solve the problem. The current (bash-4.3-devel) code works something like this, assuming no errors (lib/readline/histfile.c:history_do_write()): rename (histfile, histfile~) open file with O_CREAT|O_TRUNC malloc buffer large enough to hold all history data write all of the history entries in one write(2) call close file unlink (histfile~) The bash-4.2 code works the same way except that it does not back up the history file. Each shell does the same thing when it exits, assuming histappend is not set, as in your configuration. There are a couple of ways the history file can end up zero-length: the malloc can fail, or the write can fail. In bash-4.2, it's too late to do anything about the truncated history file at that point. In bash-4.3, the previous history file will be restored. The O_EXCL solution could work, to some extent, in bash-4.3. It won't do any good in bash-4.2 because it will cause open() to fail under the perfectly reasonable circumstance of an existing history file. What have I missed that could cause file truncation due to a race condition? Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRU c...@case.edu http://cnswww.cns.cwru.edu/~chet/