Closing a tab or the window does not result in a logout #10895

Closed
opened 2026-01-31 02:33:06 +00:00 by claunia · 3 comments
Owner

Originally created by @doomchild on GitHub (Oct 5, 2020).

I have Debian installed (uname -a returns Linux Arcadia 4.19.104-microsoft-standard #1 SMP Wed Feb 19 06:37:35 UTC 2020 x86_64 GNU/Linux), and I was in the process of getting all of my SSH stuff setup when I noticed that my .bash_logout (where I kill the instance of ssh-agent that was started on login) wasn't getting executed if I closed a tab or exited Windows Terminal. After a couple of opens and closes, I've got five or six instances of ssh-agent -s running.

If I explicitly call logout, the window closes and I can open a new one to see that the previous instance was correctly killed as desired.

Environment

Windows build number: 10.0.19042.0
Windows Terminal version (if applicable): 1.1.2233.0

Steps to reproduce

Add the following to ~/.bashrc:

if [ -z "$SSH_AUTH_SOCK" ] ; then
  eval `ssh-agent -s`
fi

Add the following to ~/.bash_logout

if [ -n "$SSH_AUTH_SOCK" ] ; then
  eval `/usr/bin/ssh-agent -k`
fi

Close Windows Terminal.
Open Windows Terminal.
Close Windows Terminal.
Open Windows Terminal.

Execute ps aux | grep ssh-agent.

Expected behavior

There should be only one instance of ssh-agent running.

Actual behavior

There are multiple instances of ssh-agent still running.

Originally created by @doomchild on GitHub (Oct 5, 2020). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> <!-- This bug tracker is monitored by Windows Terminal development team and other technical folks. **Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**. Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue. If this is an application crash, please also provide a Feedback Hub submission link so we can find your diagnostic data on the backend. Use the category "Apps > Windows Terminal (Preview)" and choose "Share My Feedback" after submission to get the link. Please use this form and describe your issue, concisely but precisely, with as much detail as possible. --> I have Debian installed (`uname -a` returns `Linux Arcadia 4.19.104-microsoft-standard #1 SMP Wed Feb 19 06:37:35 UTC 2020 x86_64 GNU/Linux`), and I was in the process of getting all of my SSH stuff setup when I noticed that my `.bash_logout` (where I kill the instance of `ssh-agent` that was started on login) wasn't getting executed if I closed a tab or exited Windows Terminal. After a couple of opens and closes, I've got five or six instances of `ssh-agent -s` running. If I explicitly call `logout`, the window closes and I can open a new one to see that the previous instance was correctly killed as desired. # Environment ```none Windows build number: 10.0.19042.0 Windows Terminal version (if applicable): 1.1.2233.0 ``` # Steps to reproduce <!-- A description of how to trigger this bug. --> Add the following to ~/.bashrc: ``` if [ -z "$SSH_AUTH_SOCK" ] ; then eval `ssh-agent -s` fi ``` Add the following to ~/.bash_logout ``` if [ -n "$SSH_AUTH_SOCK" ] ; then eval `/usr/bin/ssh-agent -k` fi ``` Close Windows Terminal. Open Windows Terminal. Close Windows Terminal. Open Windows Terminal. Execute `ps aux | grep ssh-agent`. # Expected behavior <!-- A description of what you're expecting, possibly containing screenshots or reference material. --> There should be only one instance of `ssh-agent` running. # Actual behavior <!-- What's actually happening? --> There are multiple instances of `ssh-agent` still running.
Author
Owner

@DHowett commented on GitHub (Oct 5, 2020):

Is this not what happens when you close an xterm or gnome-terminal? I am not certain that shells typically react to SIGHUP by processing their logout scripts.

Does this work as expected elsewhere?

@DHowett commented on GitHub (Oct 5, 2020): Is this not what happens when you close an xterm or gnome-terminal? I am not certain that shells typically react to SIGHUP by processing their logout scripts. Does this work as expected elsewhere?
Author
Owner

@doomchild commented on GitHub (Oct 5, 2020):

My Debian box autostarts ssh-agent via x-session-manager, so I can't test this exact scenario. However, one big difference is that if I try to logout on that box, I get bash: logout: not login shell: use 'exit', whereas on WSL2, if I enter logout, the tab closes (or the window if it's the only tab). If Windows Terminal is opening a login shell, shouldn't .bash_logout be run when the window is closed?

@doomchild commented on GitHub (Oct 5, 2020): My Debian box autostarts `ssh-agent` via `x-session-manager`, so I can't test this exact scenario. However, one big difference is that if I try to `logout` on that box, I get `bash: logout: not login shell: use 'exit'`, whereas on WSL2, if I enter `logout`, the tab closes (or the window if it's the only tab). If Windows Terminal is opening a login shell, shouldn't `.bash_logout` be run when the window is closed?
Author
Owner

@DHowett commented on GitHub (Oct 5, 2020):

So, alright. There's a combination of issues here.

  • wsl -d X starts your login shell as a login shell (automatically)
  • It is not standard behavior for a shell to run logout scripts when its controlling terminal exits

This behavior can be recapitulated in gnome-terminal if you toggle the "start ... as login shell" option: exiting the tab unceremoniously does not result in .bash_logout running.

I'm going to close this bug as by-design, but with a quick suggestion:

It may be possible to cache the output of ssh-agent such that you don't need to rely on a shell to exit gracefully and tear down the running instance. My personal agent startup looks a bit like...

function {
        function _agent_add_keys() {
                ssh-add &> /dev/null
                ssh-add ~/.ssh/id_rsa_howett.net &> /dev/null
        }

        if [[ ! -z "${SSH_AUTH_SOCK}" ]]; then
                if ! ssh-add -l &> /dev/null; then
                        _agent_add_keys
                fi
                unfunction _agent_add_keys
                return
        fi

        local _agent_env
        _agent_env="$HOME/.ssh/agent-env-$HOST"
        function _start_agent() {
                ssh-agent -s > "${_agent_env}"
                chmod 600 "${_agent_env}"
                . "${_agent_env}" &> /dev/null
                _agent_add_keys
        }


        ssh-add -l &> /dev/null
        if [[ $? -eq 2 ]]; then
                [[ -f "${_agent_env}" ]] && . "${_agent_env}" &> /dev/null
                ssh-add -l &> /dev/null
                if [[ $? -eq 2 ]]; then
                        _start_agent
                fi
        fi

        unfunction _start_agent
        unfunction _agent_add_keys
}

(admittedly, this is for zsh instead of bash.) It caches the agent info in ~/.ssh/agent-env-LIBRA or whatever my machine name suggests it should be, and each instance of my shell imports that info. It detects that the agent has exited because it'll fail to add a key. If it does so, it restarts it.

This seems a little cleaner than having one agent per shell instance, and presuming that I launch a lot of shells it's a reasonable savings of resources spent.

@DHowett commented on GitHub (Oct 5, 2020): So, alright. There's a combination of issues here. * `wsl -d X` starts your login shell as a login shell (automatically) * It is not standard behavior for a shell to run logout scripts when its controlling terminal exits This behavior can be recapitulated in gnome-terminal if you toggle the "start ... as login shell" option: exiting the tab unceremoniously does not result in `.bash_logout` running. I'm going to close this bug as by-design, but with a quick suggestion: It may be possible to cache the output of `ssh-agent` such that you don't need to rely on a shell to exit gracefully and tear down the running instance. My personal agent startup looks a bit like... ``` function { function _agent_add_keys() { ssh-add &> /dev/null ssh-add ~/.ssh/id_rsa_howett.net &> /dev/null } if [[ ! -z "${SSH_AUTH_SOCK}" ]]; then if ! ssh-add -l &> /dev/null; then _agent_add_keys fi unfunction _agent_add_keys return fi local _agent_env _agent_env="$HOME/.ssh/agent-env-$HOST" function _start_agent() { ssh-agent -s > "${_agent_env}" chmod 600 "${_agent_env}" . "${_agent_env}" &> /dev/null _agent_add_keys } ssh-add -l &> /dev/null if [[ $? -eq 2 ]]; then [[ -f "${_agent_env}" ]] && . "${_agent_env}" &> /dev/null ssh-add -l &> /dev/null if [[ $? -eq 2 ]]; then _start_agent fi fi unfunction _start_agent unfunction _agent_add_keys } ``` (admittedly, this is for zsh instead of bash.) It caches the agent info in `~/.ssh/agent-env-LIBRA` or whatever my machine name suggests it should be, and each instance of my shell imports that info. It detects that the agent has exited because it'll fail to add a key. If it does so, it restarts it. This seems a little cleaner than having one agent _per shell instance_, and presuming that I launch _a lot of shells_ it's a reasonable savings of resources spent.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#10895