[Bug 278230] sh: Multiple instances overwrite each other's history

From: <bugzilla-noreply_at_freebsd.org>
Date: Sun, 07 Apr 2024 11:05:02 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=278230

            Bug ID: 278230
           Summary: sh: Multiple instances overwrite each other's history
           Product: Base System
           Version: CURRENT
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Some People
          Priority: ---
         Component: bin
          Assignee: bugs@FreeBSD.org
          Reporter: sstj6a2b@eseipi.net

Created attachment 249800
  --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=249800&action=edit
sh: Reload HISTFILE before saving history if necessary

A basic persistent history for sh(1) was implemented in 988b1bb0c54e, but it
doesn't work well when multiple shells coexist at the same time. In addition to
the common and obvious situation with two terminal emulators running
simultaneously (where history from one session will be overwritten entirely by
another), let's consider the following use case: someone starts an X/Wayland
session from a login sh(1) interactively. Then, after logging out, HISTFILE
will contain commands from the tty session only, but not from any other shells
that may have been used.

Steps to reproduce:
1) Start two sh(1) instances.
2) Run "exit 1" in the 1st one. Now the last line of the HISTFILE is "exit 1".
3) Run "exit 2" in the 2nd one. HISTFILE doesn't contain "exit 1" anymore, only
"exit 2".

It happens because sh(1) dumps only history events it has in memory, i.e. those
loaded from HISTFILE at start plus the ones entered by user in the current
session, but doesn't care that HISTFILE content may have changed in the
meantime.

I wrote a patch that seems to solve the problem for me, but not sure if this
can be considered as expected behavior for anyone else.

-- 
You are receiving this mail because:
You are the assignee for the bug.