Bug 185140 - Konsole should send EOF before closing tabs
Summary: Konsole should send EOF before closing tabs
Status: RESOLVED FIXED
Alias: None
Product: konsole
Classification: Applications
Component: general (show other bugs)
Version: unspecified
Platform: unspecified Linux
: NOR wishlist
Target Milestone: ---
Assignee: Konsole Developer
URL:
Keywords:
: 197875 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-02-21 17:14 UTC by kirrus
Modified: 2021-09-06 16:40 UTC (History)
11 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description kirrus 2009-02-21 17:14:53 UTC
Version:            (using KDE 4.2.0)
OS:                Linux
Installed from:    I Don't Know

Originally posted on Launchpad:
https://bugs.launchpad.net/ubuntu/+source/kdebase/+bug/298480


Summary:
Konsole should not close background apps (processes called by "command &") upon closing konsole window.

To reproduce:
Open console
Type in kate &
Close konsole window
Kate window also closes

Currently, gnome-terminal supports this feature.
Comment 1 Matthew Woehlke 2009-03-10 20:15:08 UTC
...then gnome-terminal is broken. Allowing jobs to run after the parent dies is creating orphans. (Whether gnome-terminal fails to shut down the shell in this case, or has a misfeature to detect still-running jobs and disown them, neither is a good idea.) To understand why this is bad, consider if the child process is a CLI program that needs an associated tty; you now have a child running that is useless but can't be stopped except by searching the system process list for it and using 'kill'. Neither the shell nor the terminal emulator should be second-guessing the user this way.

Fortunately, there are already ways to indicate that you wish a child process to ignore termination of the parent. The canonical way is 'nohup'. In bash, it's often easier to 'disown' such children (especially if you forgot to use 'nohup' to start the child), i.e. 'kate & disown'.

(Having said that, it would be nice if konsole would integrate detachable sessions, mainly so that crashes wouldn't take out all my shell sessions, but that's a different issue.)

@Robert: I'm closing this WONTFIX. Obviously as the maintainer you can decide that's wrong :-), but IMO "fixing" the "bug" exactly as written would be broken.
Comment 2 Robert Knight 2009-03-10 23:08:16 UTC
No, I agree with the reporter about what I expect to happen - and it does for me.  If I start a Konsole session, run kate& and quit the session and also Konsole then Kate stays running as I expect.
Comment 3 Robert Knight 2009-03-10 23:09:10 UTC
Also, about accidentally exiting with running jobs - the shell will, depending on its current settings, warn you about that.
Comment 4 Robert Knight 2009-03-10 23:12:18 UTC
I was able to reproduce.  Normally I close tabs by pressing Ctrl+D to tell the shell to exit - in this case, kate stays running in the 'background'.  If on the other hang I put kate in the background and close Konsole via File -> Close Window then Kate exits.
Comment 5 Matthew Woehlke 2009-03-12 00:07:20 UTC
Right, I had no trouble reproducing, but I don't see how you can fix it other than to either a: not sighup bash, or b: rely on detecting bash complaining about stopped jobs, or...

Eew. I tried your way; apparently on EOF, bash happily disowns anything in the background without warning? That feels icky.

Ah, well, I guess the solution then is that Konsole should always try to kill shells with EOF before resorting to SIGHUP. I'm still not sure how I feel about that, though; what if I am running a process where EOF will cause something to happen that I don't want, but SIGHUP would be fine?

(Meanwhile, I have asked Chet if this is desired behavior. Alas, I would have to install another shell to check if this is typical.)
Comment 6 Matthew Woehlke 2009-03-12 23:06:10 UTC
Hmm... well, I still don't really like this behavior, but Chet has spoken: http://permalink.gmane.org/gmane.comp.shells.bash.bugs/12602.

So according to him, Konsole should send EOF when anything (tab or whole app) is closed "nicely".
Comment 7 Matthew Woehlke 2009-06-26 18:20:40 UTC
*** Bug 197875 has been marked as a duplicate of this bug. ***
Comment 8 Jekyll Wu 2011-10-24 14:28:58 UTC
(In reply to comment #6)

The unfortunate fact is currently most emulators do not. From my observation, xterm/vte/uxvt-unicode/mlterm all send SIGHUP thus killing background processes, while putty is the only one which sends EOF.
Comment 9 ggrocca 2014-02-04 22:53:15 UTC
What's the situation on this bug? I returned to KDE after a long, long pause (and I'm very happy with it I have to say) but yesterday I lost several paragraphs on my text editor because I started it from konsole and it quitted without confirm when I close konsole by click instead of ctrl-D.

Long story short: I confirm the bug it's still there, 2014, version 2.12.1
Comment 10 Martin Sandsmark 2016-07-11 10:59:41 UTC
Git commit 2f1c8cf397be6c91fcd60234740d9727650c1a87 by Martin T. H. Sandsmark.
Committed on 11/07/2016 at 10:57.
Pushed by sandsmark into branch 'master'.

Send EOF when closing first, before SIGHUP

According to the bash maintainer sending EOF is the appropriate way of
telling e. g. bash to quit:
    http://permalink.gmane.org/gmane.comp.shells.bash.bugs/12602

I wasn't able to get KPtyProcess to send that in a nice way (calling
setStandardInputFile(QProcess::nullDevice(), for example), so we fetch
the EOF character manually with tcgetattr and send that.
REVIEW: 128416

M  +16   -0    src/Pty.cpp
M  +5    -0    src/Pty.h
M  +9    -0    src/Session.cpp

http://commits.kde.org/konsole/2f1c8cf397be6c91fcd60234740d9727650c1a87
Comment 11 Markus Trippelsdorf 2016-08-24 15:43:56 UTC
This completely breaks tmux (or screen). 
The currently active window gets the EOF is therefore closed whenever I close Konsole.
When I re-attach to the tmux session the window is of course gone.
This is unacceptable.
Comment 12 Oswald Buddenhagen 2021-08-01 12:11:20 UTC
for posterity: this change was fundamentally misguided (as per matthew's first comment). as always when deviating from decades of established protocol, this broke several things, like the already mentioned tmux (which was worked around later) and bug #401898.
Comment 13 Ahmad Samir 2021-08-01 13:10:45 UTC
The gmane link seems to be broken; digging around in the bash bugs mailing list archives, I think this is the thread linked in comment#6:
https://lists.gnu.org/archive/html/bug-bash/2009-03/msg00089.html
Comment 14 Ahmad Samir 2021-08-01 13:32:14 UTC
This is a complicated issue... so I started by testing this a bit:
- first case:
kwrite &
Ctrl+D

- second case:
kwrite &
closing window using the close button on the titlebar


With konsole, kwrite stays running in both cases.

With xterm, gnome-terminal or terminology:
- kwrite stays running
- kwrite is killed

so konsole is the odd one out.
Comment 15 Ahmad Samir 2021-08-01 14:08:58 UTC
So far it looks like:
- it would be better to not send EOF manually, if the user sends an EOF when closing, then that's a the "nice" way of closing a shell (bash specifically, since that's what the links in this report were talking about) and a background process would stay running
- otherwise we send SIGHUP, and a background process (if any) is killed

The user can always use `foo & disown`, if the requirement is to always leave a background process running, regardless of how the terminal emulator window is closed.
Comment 16 Bug Janitor Service 2021-08-01 16:41:17 UTC
A possibly relevant merge request was started @ https://invent.kde.org/utilities/konsole/-/merge_requests/449
Comment 17 Christoph Cullmann 2021-08-01 16:41:58 UTC
Merge request to revert the ancient change (and the later done workarounds to avoid other issues like closing tmux) added.
Comment 18 Ahmad Samir 2021-08-13 12:58:03 UTC
Git commit 3d967a597472a75b2afbd75afb20d7f44a3881f2 by Ahmad Samir, on behalf of Christoph Cullmann.
Committed on 13/08/2021 at 12:57.
Pushed by ahmadsamir into branch 'master'.

improve session closing behavior

the new session close behavior is again the old

- first send SIGHUP and wait one second if that works
- then hard kill

before we tried to send an EOF character
this needed workaround to not kill "non shell" programs
unexpected and doesn't work e.g. if

- you have some not finished command input in your shell
- on e.g. FreeBSD in general

When closing the session with some background process still running:
- using Ctrl+D (which sends EOF) will leave that background process running
  (which is the expected behaviour with e.g. BASH:
  https://lists.gnu.org/archive/html/bug-bash/2009-03/msg00091.html)
- using e.g. the close button on the titlebar will close the session and kill any background processes

You can also use 'somecommand & disown', if you want to ensure background processes
aren't killed when you close a konsole window.
Related: bug 401898

M  +0    -16   src/Pty.cpp
M  +0    -5    src/Pty.h
M  +7    -18   src/session/Session.cpp

https://invent.kde.org/utilities/konsole/commit/3d967a597472a75b2afbd75afb20d7f44a3881f2
Comment 19 Kurt Hindenburg 2021-09-06 16:40:04 UTC
Git commit 18dc0fde2b47b70ac65e4a0400ed0b79c12b1196 by Kurt Hindenburg, on behalf of Christoph Cullmann.
Committed on 06/09/2021 at 16:35.
Pushed by hindenburg into branch 'release/21.08'.

improve session closing behavior

the new session close behavior is again the old

- first send SIGHUP and wait one second if that works
- then hard kill

before we tried to send an EOF character
this needed workaround to not kill "non shell" programs
unexpected and doesn't work e.g. if

- you have some not finished command input in your shell
- on e.g. FreeBSD in general

When closing the session with some background process still running:
- using Ctrl+D (which sends EOF) will leave that background process running
  (which is the expected behaviour with e.g. BASH:
  https://lists.gnu.org/archive/html/bug-bash/2009-03/msg00091.html)
- using e.g. the close button on the titlebar will close the session and kill any background processes

You can also use 'somecommand & disown', if you want to ensure background processes
aren't killed when you close a konsole window.
Related: bug 401898
(cherry picked from commit 3d967a597472a75b2afbd75afb20d7f44a3881f2)

M  +0    -16   src/Pty.cpp
M  +0    -5    src/Pty.h
M  +7    -18   src/session/Session.cpp

https://invent.kde.org/utilities/konsole/commit/18dc0fde2b47b70ac65e4a0400ed0b79c12b1196