5

Recently (possibly with the Sonoma update, but possibly even more recently than 14.0), Apple Terminal has been giving the following error on startup (when macOS is set to restore windows, i.e., when System Settings > Close windows when quitting… is unset):

date: illegal time format
usage: date [-jnRu] [-I[date|hours|minutes|seconds]] [-f input_fmt]
            [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]
            [[[[mm]dd]HH]MM[[cc]yy][.SS] | new_date] [+output_fmt]
-bash: Saving: command not found

or, in the case of zsh,

/Users/jaffe/.zsh_sessions/<random hex guid>.session:2: command not found: Saving

Since originally posting this question (see updates below), it has become clear that the error appears to happen in multiple versions of macOS, on Intel and Apple Silicon. It does not appear to be due to installed software or config files.

I have traced this to an error coming from /etc/bashrc_Apple_Terminal (or /etc/zshrc_Αpple_Terminal) which is largely concerned with saving and restoring sessions for the various terminal tabs and windows. (Most of the following will specifically refer to the symptoms with bash but it’s similar for zsh.)

It comes from the lines

    if [ -r "$SHELL_SESSION_FILE" ]; then
    . "$SHELL_SESSION_FILE"
    rm "$SHELL_SESSION_FILE"
    fi

On shell startup, this snippet runs, and then deletes, "$SHELL_SESSION_FILE", which is written at shell exit by the following lines:

    if [ -n "$SHELL_SESSION_FILE" ]; then
        echo -ne '\nSaving session...' >&2
        (umask 077; echo 'echo Restored session: "$(/bin/date -r '$(/bin/date +%s)')"' >| "$SHELL_SESSION_FILE")
        declare -F shell_session_save_user_state >/dev/null && shell_session_save_user_state
        shell_session_history_allowed && shell_session_save_history
        echo 'completed.' >&2
    fi

The error occurs because $SHELL_SESSION_FILE incorrectly contains the following text:

Saving session...echo Restored session: "$(/bin/date -r 1698759014 Saving session...)"

rather than echo Restored session: "$(/bin/date -r 1698759014) For some reason, the Saving session... text is appearing in the file (twice!) -- it is being sent to "$SHELL_SESSION_FILE" rather than stderr (via >&2).

I think I didn't make any changes to cause this, but perhaps I am wrong. The directory ~/bash_sessions/ seems to have recent files with this error, and slightly older files (as recent as yesterday!) which don't. I regularly update various packages with homebrew, and I have recently installed Python 3.12 from python.org, but I can't see how they could reach into the guts of shell startup.


Update: In fact, I see this even with a new user, and even if I use zsh rather than bash! (Apple Terminal has very similar code for session history management under zsh.) So it seems that some other recent change to my system is causing this. But I suspect it's not simply a Sonoma problem, since it does not appear to be widespread. Yikes!

Update 1a: Note that the code is run via trap shell_session_update EXIT. The error only seems to happen when I exit the shell by quitting Terminal.app, but not when I just use the exit command. (And I've even tried explicitly sending various signals to the shell via kill -n, but I can't seem to induce the error that way.)

Update 2: At least one other person is seeing this! Perhaps this indicates an issue with VS Code?

Update 3: After some back and forth in the comments below, it is clear that the error spans multiple versions of macOS on both Intel and Apple Silicon architectures, and we seem to have eliminated VS Code and possibly Python (we seem to be using different versions and installers) as the cause; I have removed /opt/homebrew and rebooted and still seeing the issue. Is it possible that any of these things somehow "pollutes" the environment -- e.g., /etc/ -- in some way that remains in place?

Update 4: Inspired by an interaction over on TidBits, I tried using Howard Oakley's excellent sandboxed lightweight VM, ViableS. I installed nothing, nada, zip, and added no files to ~/ or anywhere else. But after starting and restarting Terminal.app with a few tabs, and ensuring that "close windows when quitting" is unset, I immediately got the message

/Users/jaffe/.zsh_sessions/<random hex guid>.session:2: command not found: Saving

This seems to point, nearly conclusively, to a macOS bug. But I still don't understand how there are people reporting not seeing it.


Is anyone else seeing this? Is anyone explicitly not seeing it? Any ideas what's happening (or even debugging suggestions)?

I've asked a more specific version of this question over on Unix & Linux SA.

Andrew Jaffe
  • 326
  • 1
  • 4
  • 22
  • 1
    If you make a new user on the Mac, does it too emit that error? My hunch is your user profile has some startup file and not that the system is broken and this is a quick test to triage and narrow down where to look. – bmike Oct 31 '23 at 14:00
  • @bmike, Yeah, I don't think it's an OS-level error, but I still can't diagnose it! On my usual user account, it still happens even if I get rid of all my .bash* and .profile files. Even on a new account, I can also make it happen... although it does seem to happen only after I've tried to use the same startup files, even if I then delete them. However, it is kind of "non-deterministic" -- it doesn't happen all of the time. – Andrew Jaffe Nov 01 '23 at 11:04
  • Please don’t crosspost on several SE sites. And especially don‘t post question pairs which need to be both read to get the full picture. If you want us to migrate this question to Linux/Unix, just flag it. – nohillside Nov 01 '23 at 14:00
  • @nohillside Thanks. The questions are sufficiently different -- this one is more about the macOS-specific symptoms, the other one is more about the underlying bash code. There is some duplication, but I feel that the questions -- and the audiences for them -- are sufficiently different to warrant remaining in both places. – Andrew Jaffe Nov 01 '23 at 14:05
  • They basically ask the same thing (why does it happen and how can I fix it) and only are relevant for macOS. Yes, they are on-topic in both sides, but please decide for one site to avoid having split discussions about it. – nohillside Nov 01 '23 at 14:28
  • How does your PATH look like? – nohillside Nov 03 '23 at 13:23
  • For the new user, it's just PATH=/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Library/TeX/texbin which is just macOS-standard plus the contents of /usr/local/bin (basically just python and a few other self-installed items) and the TeX-related stuff at the end. I don't think there is anything untoward. – Andrew Jaffe Nov 03 '23 at 13:48
  • 1
    I would worry about /usr/local/bin, there may be commands there which shadow the Apple ones and have different options. – nohillside Nov 03 '23 at 14:37
  • There is nothing weird in /usr/local/bin (Python, GitHub, bbedit, textmate, R, wolframscript). Perhaps something at startup? – Andrew Jaffe Nov 10 '23 at 13:38
  • If the issue can be avoided by using a lock file (as an answer implies I saw somewhere) or by simply skipping the initial echo, the problem most likely is within *_Apple_Terminal or related to several tabs closing at once. – nohillside Nov 10 '23 at 13:42
  • @nohillside Agreed! But why does it not appear to be more widespread? (I personally only started seeing it recently, but I have been opening and closing Apple Terminal with many tabs and windows forever!). – Andrew Jaffe Nov 10 '23 at 13:49
  • (And as a further datapoint: I just rebooted and went straight to a user with no startup files at all. I could reproduce the bug by starting, quitting and restarting Apple Terminal.app.) – Andrew Jaffe Nov 10 '23 at 13:50
  • Interesting. Did said user have previous sessions stored? – nohillside Nov 10 '23 at 13:51
  • No, I first deleted everything in the .zsh_sessions dir. (So the first time, there was no bug, but it appeared on a quit and restart of Terminal.app.) – Andrew Jaffe Nov 10 '23 at 13:52
  • Just tried the same, couldn't reproduce the problem. But then the restarted Terminal also didn't restore the session. Hmm. – nohillside Nov 10 '23 at 14:02
  • You need to unset Settings > “Desktop & Dock”: > Windows > “Close windows when quitting an application”. – Andrew Jaffe Nov 10 '23 at 14:03
  • Right. I can trigger if with one tab open in Terminal. If I have two open tabs and restart Terminal, the error doesn't trigger. – nohillside Nov 10 '23 at 14:11
  • 1
    I've got exactly the same problem with iMac, macOS version 12.7.1. And my MacBook with macOS 14.1 works fine (very similar setup otherwise). – Jukka Suomela Nov 11 '23 at 18:42
  • @JukkaSuomela have you unset Settings > “Desktop & Dock”: > Windows > “Close windows when quitting an application” on both machines? Do you have the same python setup? VS Code? (I see it in two 14.1 machines) – Andrew Jaffe Nov 11 '23 at 21:23
  • @AndrewJaffe "Close windows when quitting an application" is enabled on both machines. Same Python setup (from Homebrew). VS Code setup should also be the same. – Jukka Suomela Nov 12 '23 at 20:29
  • 1
    @AndrewJaffe iMac had VS Code 1.84.1, Macbook had VS Code 1.84.0. Upgraded Macbook to VS Code 1.84.2, rebooted, and now I'm also seeing the familiar error message there. – Jukka Suomela Nov 12 '23 at 20:41
  • Interesting, FWIW, I have deleted VS Code on one of my machines but the problem still appears. But this could just meant that it somehow pollutes system startup? Or it could be a red herring... – Andrew Jaffe Nov 12 '23 at 21:31
  • 1
    I don’t have VSCode installed. – nohillside Nov 12 '23 at 21:36
  • Ok, I’m at a loss. What do we have in common? Are we all using homebrew? On Apple silicon, and hence in /opt? – Andrew Jaffe Nov 13 '23 at 16:53
  • 1
    I use Homebrew on M1, but the account I used to reproduce the issue used the standard zsh version. – nohillside Nov 13 '23 at 17:34
  • 1
    Homebrew, Intel. – Jukka Suomela Nov 13 '23 at 17:53
  • @agarza (Meta) I rolled back your changes -- I prefer italics to bold, and I think that the "audit trail" of "Updates" is useful (and the structure makes more sense knowing that they are updates; I could rewrite the entire question including that information, but then some of the answers would not make sense!) – Andrew Jaffe Jan 08 '24 at 15:22
  • 1
    @AndrewJaffe Italics make it difficult to read for those with visual imparities such as dyslexia. – agarza Jan 08 '24 at 15:26
  • 1
    @agarza, I was unaware of that. Will convert back to bold. – Andrew Jaffe Jan 08 '24 at 15:27

4 Answers4

2

Since I've been asked to do so in .zsh_sessions producing an error I post here the fact that commenting out the echo line did make the problem disappear in a similar case:

I commented out the echo -ne line in etc/zshrc_Apple_Terminal and the problem disappeared

  shell_session_save() {
    # Save the current state.
    if [ -n "$SHELL_SESSION_FILE" ]; then
        # echo -ne '\nSaving session...' >&2
        (umask 077; echo 'echo Restored session: "$(/bin/date -r '$(/bin/date +%s)')"' >| "$SHELL_SESSION_FILE")

As per a user suggestion, so does replacing it by

print -u 2 $'\nSaving session ...' 
Hugues
  • 523
  • 1
    This solves the symptom, but it doesn't really give any insight into why the problem is occurring (which we still don't know!). And even with something like this solution (which I have also tried) I actually still get similar errors tracing back to the printf statement elsewhere in bashrc_Apple_Terminal. – Andrew Jaffe Nov 08 '23 at 14:43
  • I'll also point out that a slightly more robust solution is to use a lock file. See https://stackoverflow.com/a/77424721/12266 – Andrew Jaffe Nov 08 '23 at 14:44
  • 2
    @AndrewJaffe Any answer helping users to avoid the problem til Apple fixes it is welcome. – nohillside Nov 08 '23 at 14:51
  • 1
    @nohillside, Agreed, although what still isn't clear is whether this is (only) an Apple problem -- in which case maybe we can expect a fix from them -- or is it that those of us who are seeing it all have (e.g.) some common 3d-party software -- in which case we might be on our own! (I have actually filed a bug using Feedback Assistant) – Andrew Jaffe Nov 08 '23 at 15:02
  • @AndrewJaffe In the newly created account you tested this with, did you start any additional applications (e.g. Visual Studio)? – nohillside Nov 08 '23 at 15:08
  • Also I'm not quite sure the rest of the zshrc_Apple_Terminal program is working as expected. I still have 2 days-old sessions in my /Users/myname/.zsh_sessions/ which I don't think should be there. – Hugues Nov 08 '23 at 15:10
  • @Hugues You might want to delete the old ones and start from scratch. – Andrew Jaffe Nov 08 '23 at 15:30
  • @nohillside I didn't start anything new with that user -- but I also wasn't on a pristine system. I just switched from one user which had experienced the problem to a new one. So it's hard to tell if it is just a global phenomenon which is switched on by some action (even by only one user) and then can't be turned off. – Andrew Jaffe Nov 08 '23 at 15:33
  • 1
    @AndrewJaffe The shell functions run for a specific user, so cross-user dependencies are unlikely. And if you only start one shell for a user, timing issues (which locking would solve) seem unlikely as well. Not sure we clarified the /usr/local/bin thing, is there anything in there which is also called from the session functions? – nohillside Nov 08 '23 at 18:24
  • I just noticed that which python3 returns me /usr/local/bin/python3 while earlier it returned /Library/Frameworks/Python.framework/Versions/3.11/bin/python3 – Hugues Nov 09 '23 at 01:02
  • 1
    @Hugues, that directory (…Frameworks…/bin) is added to your path when you install Python from python.org, which explicitly changes your bash startup files — look in /Applications/Python\ 3.12 — whereas /usr/local/bin is probably homebrew Python. (Did you edit your startup files yourself in your attempts to fix this and ended up changing your path?) What’s interesting to me is that I recently switched from homebrew Python to the one downloaded from Python.org, and that is around when I started seeing these issues. So perhaps it is related? – Andrew Jaffe Nov 09 '23 at 07:26
  • @AndrewJaffe Thanks for the info. You're right, I did edit the $PATH variable in .zprofile, after noticing the bug we're dealing with, and after I realised I had 3.11 installed by the system and thought I did not need a personalised one. This to avoid parsing all python versions for each zsh function call. I suspect I unknowingly messed with the python version when I installed an open source software called Musescore. I did not perform myself any installation of python recently. I'm not a developer so please excuse my being vague on what I did exactly – Hugues Nov 09 '23 at 14:56
  • @AndrewJaffe So to sum it up, the bug is not linked to a specific Mac OS version, it is not linked to the 3.12 or 3.11 python version by itself, it does not seems to be either linked to the source (python.org, or homebrew) since the bug is not present on another account of mine that uses /usr/local/bin/python3 too (which redirects to (…Frameworks…/bin) which is also the location of the version provided by Apple). – Hugues Nov 09 '23 at 15:29
1

Since you have determined that your startup files are causing this (by testing a new user account without any customizations), I would take these next approaches.

  1. Run your modified files through a linter. Shell check is excellent in my experience: https://www.shellcheck.net/
  2. Set debug flag for bash early in your script.
  3. Closely look at any use of the variable you identified as getting stomped.

For item 2, perhaps you can set an alternate shell temporarily and then call bash:

/bin/bash -lixc exit 2>&1 | less

if your <GUID.session> file is unclean - we'll need to pick apart why your customizations are breaking that file generation in the function shell_session_save - on my system this is as follows:

me@mac ~ % which shell_session_save
shell_session_save () {
    if [ -n "$SHELL_SESSION_FILE" ]
    then
        echo -ne '\nSaving session...' >&2
        (
            umask 077
            echo 'echo Restored session: "$(/bin/date -r '$(/bin/date +%s)')"' >| "$SHELL_SESSION_FILE"
        )
        whence shell_session_save_user_state > /dev/null && shell_session_save_user_state "$SHELL_SESSION_FILE"
        local f
        for f in $shell_session_save_user_state_functions
        do
            $f "$SHELL_SESSION_FILE"
        done
        shell_session_history_allowed && shell_session_save_history
        echo 'completed.' >&2
    fi
}

What’s curious is zsh is also reported to have a curiously similar issue.

bmike
  • 235,889
  • 1
    Well, I know exactly where the error occurs: it's in the line echo -ne '\nSaving session...' >&2, incorrectly writing to a file instead of stderr (although I think there are some rare occasions where other incorrect characters are being written to $SHELL_SESSION_FILE). And although the error seems to require (my?) startup files, I'm not completely sure that it's the files themselves that are causing the problems. – Andrew Jaffe Nov 01 '23 at 22:46
  • Quite the mystery - I'm glad you know exactly where it occurs - it's hard for me to guess from here @AndrewJaffe but perhaps my thinking will help you over the gap between now and a fix. – bmike Nov 01 '23 at 22:52
  • Interesting -- your version of shell_session_save is slightly different. But I think perhaps you're on zsh rather than bash (which doesn't have whence). Maybe it's finally time to switch to zsh? – Andrew Jaffe Nov 01 '23 at 23:01
  • 1
    correct - I should have changed my shell to get that / got carried away with calling bash from zsh... @AndrewJaffe – bmike Nov 01 '23 at 23:18
  • 1
    Got a similar issue, likely from the same shell_session_save line when using zsh. https://apple.stackexchange.com/questions/466160/zsh-sessions-producing-an-error?noredirect=1#comment689770_466160 – Hugues Nov 08 '23 at 00:20
  • @Hugues, what do you have on your machine? Python? Homebrew? VS Code? Did this behaviour start suddenly? – Andrew Jaffe Nov 08 '23 at 10:13
  • Yes. Python & Homebrew. It started after the 26th of October I believe. Correlated but maybe unrelated with the installation of MuseScore software on the 27th – Hugues Nov 08 '23 at 14:00
  • @Hugues, out of curiosity, which install of python? Via homebrew, or the python.org package, or conda? – Andrew Jaffe Nov 08 '23 at 14:44
  • The latest is 3.11.1 which I believe I is the default now in OX X 12.7.1. I also have all the others from 3.7 from various sources, homebrew and anaconda I believe – Hugues Nov 08 '23 at 15:05
  • @Hugues are you back on macOS 12? I’m on Sonoma (15) and certainly didn’t experience this back when I was on 12. So this indicates it’s not strictly a macOS issue. – Andrew Jaffe Nov 09 '23 at 08:50
  • 1
    Yes, I'm still on 12.7.1, as I did not want to upgrade for various reasons. It could however still be a MacOs X issue as there are regular updates for security reasons and there was one in October. – Hugues Nov 09 '23 at 14:44
1

I saw this today! Haven't seen it before. I'm on Monterey (12.7.1 21G920).

I rebooted my MacBook today (not installing the update, just a normal reboot). After it restarted, I noticed that it didn't properly restore all my Terminal tabs, but instead restored the state from around Nov 2.

I think I quit the Terminal later again, and after I opened it again, I now saw this in every restored tab:

/Users/kuba/.zsh_sessions/C6E94843-05DC-43CC-933E-5190FC8E0825.session:2: command not found: Saving

I haven't seen that message before. I have a suspicion that it might have something to do with why the Terminal restored tabs state from Nov 2 instead of from before reboot… perhaps they've changed the way how sessions are stored in some recent update? (updated zsh to some new version?)

(More info: I don't have VS Code installed, I have Homebrew installed, but not many packages in it, and also MacPorts.)

1

I think I figured this out. Running multiple Terminal sessions at shutdown can cause a race condition in writing the history file. See my reply https://stackoverflow.com/questions/62316487/terminal-modified-permanently-by-vscode/77424721#77424721

But it doesn't have anything to do with VSCode; it's an Apple bug. Here's my fix:

scott@Mac-mini-x86 etc $ diff  bashrc_Apple_Terminal.orig bashrc_Apple_Terminal 
215,219c215,223
<       echo -ne '\nSaving session...' >&2
<       (umask 077; echo 'echo Restored session: "$(/bin/date -r '$(/bin/date +%s)')"' >| "$SHELL_SESSION_FILE")
<       declare -F shell_session_save_user_state >/dev/null && shell_session_save_user_state
<       shell_session_history_allowed && shell_session_save_history
<       echo 'completed.' >&2
---
>       local save_lock_file="$SHELL_SESSION_DIR/_saving_lockfile"
>       if /usr/bin/shlock -f "${save_lock_file}" -p $$; then
>           (umask 077; echo 'echo Restored session: "$(/bin/date -r '$(/bin/date +%s)')"' >| "$SHELL_SESSION_FILE")
>           echo -ne '\nSaving session...' >&2
>           declare -F shell_session_save_user_state >/dev/null && shell_session_save_user_state
>           shell_session_history_allowed && shell_session_save_history
>           echo 'completed.' >&2
>           /bin/rm "${save_lock_file}"
>       fi

Note that it's pretty trivial; it just locks to serialize updating of the history file.

  • I can reproduce the problem with one Terminal session/one tab only. – nohillside Dec 11 '23 at 05:07
  • Somewhat anecdotally, I am not seeing this with only one session. (But you might try deleting the files in ~/.bash_sessions or ~/.zsh_sessions first.) This interpretation makes some sense, but it is still weird that (a) it seems to have only started recently (but on multiple OS versions?); (b) the error is (almost) always the same, so the incorrect characters are always going in the same place. – Andrew Jaffe Dec 11 '23 at 12:28
  • Disappointingly (but not unexpectedly) macOS 14.2 does not address this issue. Which raises another point (c): If this is a macOS bug, how widespread is it? In the few cases I've checked w/ other people, the bug does appear. So I would imagine that even developers actually working at Apple — who must use Terminal, right?! — would see this bug (and then want it fixed)! Or does it depend on something else that we've all got in common but haven't figured out yet? – Andrew Jaffe Dec 11 '23 at 22:00
  • I'm no longer sure that it's multiple Terminal sessions, although it is definitely a race condition writing the history file. But the most significant development to watch for is that ANY MACOS UPDATE will overwrite /etc/bashrc_Apple_Terminal back to its buggy state from whatever fix you may have made! Boo! C'mon Apple! It's a small, safe fix~ – Scott Marks Dec 16 '23 at 16:48
  • 1
    Also note that if your history file is messed up, it will continue to spew these errors until the process of starting and ending Terminal sessions truncates the messed up part of the history file. So the bug will show for a while even if you are just running a single Terminal session. :( – Scott Marks Dec 16 '23 at 17:11
  • Since it won't fit in a comment, I'll reproduce the patch as a modification to my original answer. – Scott Marks Dec 16 '23 at 17:14
  • It’s a “small, safe fix” — but is it just fixing the symptom and not the cause? – Andrew Jaffe Dec 21 '23 at 07:20
  • And I’m still puzzled about how widespread the issue is. None of us can make it go away (I think). Does everyone with the appropriate settings about restoring windows see this? But in that case surely people at Apple itself must be experiencing it?! Or is there something unusual that we all have in common that we haven’t figured out?? – Andrew Jaffe Dec 21 '23 at 07:23
  • 1
    Obviously the cause is two Terminal processes in a race condition to exit. I'm not sure exactly how I feel about the answer to symptom v cause -- it could be difficult to keep different system processes that might fire up a quick bash from terminating at the same time, and since this routine is the common point where the error occurs, I lean toward calling this a fix. – Scott Marks Jan 05 '24 at 22:18
  • 1
    As for it being a small, safe fix: simply locking the history file while writing to it is small (check out the patch), certain safe (multiple racers will just be serialized), but I suppose it could step on a few msecs of performance during startup/shutdown.

    I think the most pernicious part of the bug is that is corrupts the history file in such a way that the pile(s) it leaves there continue to cause errors for a while, making it appear as if the fix doesn't fix it. And then in the sort of behavior that gives Macs their reputation for being alive, it just .... goes away.

    – Scott Marks Jan 05 '24 at 22:22
  • For what it's worth, I've tried this on a completely empty virtual machine — no software installed, no dot files — and I see the bug immediately. (See the recent "Update 4" to the main question text above) – Andrew Jaffe Jan 06 '24 at 16:38
  • Exactly! This bug is in the Apple Terminal software (more precisely, the bash scripts supporting Apple Terminal fanciness) as delivered. – Scott Marks Jan 08 '24 at 23:52
  • Well, it’s not completely clear where the underlying bug lies. After all, there is a bug somewhere causing stderr to be incorrectly sent to a file, which I don’t think is due to a problem in the script per se. – Andrew Jaffe Jan 09 '24 at 11:47