Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

distrobox-enter should terminate child processes when the terminal is closed #966

Open
1player opened this issue Sep 12, 2023 · 5 comments
Labels
bug Something isn't working wait-on-user waiting for a reply

Comments

@1player
Copy link

1player commented Sep 12, 2023

A bit of a weird one... I've been trying to figure out why host-spawn, and its children, do not terminate on my machine when I close a terminal window (1player/host-spawn#27). In theory, when you close a terminal window, a SIGHUP is sent to the controlling process, so anything that was running is terminated cleanly, which is what everyone expects.

This doesn't happen with distrobox-enter. This script calls podman exec, which ignores SIGHUP (probably because it is/was originally intended to run containers in the background), so any process running inside the container is never terminated and runs indefinitely in the background.

Steps to reproduce

  • Enter a distrobox container in a terminal window
  • Run a long running process (sleep infinity)
  • Close the terminal window
  • Notice that the long running process is still running

Expected behaviour (how it works normally)

  • Open a terminal window on the host
  • Run a long running process (sleep infinity)
  • Close the terminal window
  • Notice that the long running process has been terminated

This discrepancy is because SIGHUP, sent by the kernel when the pty closes, so that the foreground process can terminate, is being ignored by podman exec. I'm unsure what's the best approach here.

@1player 1player added the bug Something isn't working label Sep 12, 2023
@1player
Copy link
Author

1player commented Sep 12, 2023

See also: containers/podman#19944

@89luca89
Copy link
Owner

I confirm this happens with podman and docker, it does not happen with lilipod

This is because the spawned process by exec, is child of either conmon on podman, and dockerd on docker
So the exiting "exec" command does not correctly forward it to the spawned process

This does not happen on lilipod where the "exec" command is indeed the father of the spawned process

Not sure what I can do, and if it's correct to be fixed on my side

@1player
Copy link
Author

1player commented Oct 4, 2023

toolbox has been having the same issue, linking all related discussions here for cross-reference:

containers/toolbox#1207

containers/toolbox#1204

@1player 1player changed the title distrobox-enter should try to respect SIGHUP distrobox-enter should terminate child processes when the terminal is closed Oct 4, 2023
@1player
Copy link
Author

1player commented Oct 10, 2023

After some back and forth, it does seem to be a distrobox bug. Basically, podman forwards SIGHUPs it receives. But the problem is distrobox-enter basically runs a podman exec -i sh, making that sh process PID 1, which by default ignores all signals (see StackOverflow link). A work around would be to call a helper program/script that traps the SIGHUPs that podman forwards, and kill the children (or the entire process group)

See also: https://stackoverflow.com/a/60537655

@89luca89
Copy link
Owner

89luca89 commented Feb 19, 2024

After some back and forth, it does seem to be a distrobox bug. Basically, podman forwards SIGHUPs it receives. But the problem is distrobox-enter basically runs a podman exec -i sh, making that sh process PID 1, which by default ignores all signals (see StackOverflow link). A work around would be to call a helper program/script that traps the SIGHUPs that podman forwards, and kill the children (or the entire process group)

See also: https://stackoverflow.com/a/60537655

I'm not sure if that's what you're referring to, but PID1 is distrobox-init , not the bash/shell spawned by enter
So that won't be closed if you exit the terminal, because that's supposed to be stopped only by a distrobox-stop or podman stop

Doing a quick experiment with distrobox-enter, the main process is only forwarded a SIGTERM when closing the terminal:

image
image

the child bash -l process (in this case) is spawned and controlled by conmon and not by distrobox-enter

image

So I'm not sure how I can forward this SIGHUP to the bash -l if that's not actually spawned by my script

@89luca89 89luca89 added the wait-on-user waiting for a reply label Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wait-on-user waiting for a reply
Projects
None yet
Development

No branches or pull requests

2 participants