sudo-project / sudo

Utility to execute a command as another user
https://www.sudo.ws
Other
1.14k stars 209 forks source link

Delay when CTRL + C -ing a Sudo command. #339

Closed Oglo12 closed 6 months ago

Oglo12 commented 6 months ago

I am using sudo to run my package manager, but everytime I misstype a package name, I try to CTRL + C Sudo, and it takes around 7 seconds or so!

millert commented 6 months ago

Does this happen for all sudo commands or only certain ones? Can you give an exact example of a command that doesn't get interrupted properly?

AlexBaranowski commented 6 months ago

@Oglo12

Good package managers are written to handle signals. The CTRLC sends the SIGINT (number 2) signals. There are different signals - for context please read the man 7 signal or https://man.docs.euro-linux.com/EL%209/man-pages/signal.7.en.html

The signals that are supposed to "kill" or "stop" (called TERM type signals) process might be handled by a program (each program can define a set of signal handlers) except for some signals like SIGKILL (some signals are handled by the kernel).

So basically when you send the signal to the package manager like yum it does not stop at once as it has to ensure for example the following:

And much much more. It's not trivial.

I also conducted simple tests:

#!/usr/bin/env bash

sudo yum clean all
sudo yum update -y &
YUM_UPDATE_PID=$(sudo pgrep yum)
echo "yum update pid: $YUM_UPDATE_PID"
sleep 1; 
sudo kill -2 $YUM_UPDATE_PID
wait

second test - without any sudo must be run as root.

#!/usr/bin/env bash

yum clean all
yum update -y &
YUM_UPDATE_PID=$(pgrep yum)
echo "yum update pid: $YUM_UPDATE_PID"
sleep 1; 
kill -2 $YUM_UPDATE_PID
wait

Both tests are run with time bash x.sh

Result with sudo:

[Alex@NormandySR2 ~]$ time bash x.sh 
0 files removed
yum update pid: 33213
Docker CE Stable - x86_64                                                                                                                                                            0.0  B/s |   0  B     00:05    
Error: Failed to download metadata for repo 'docker-ce-stable': Librepo was interrupted by a signal

real    0m5.648s
user    0m0.531s
sys 0m0.174s

Result without sudo:

[root@NormandySR2 ~]# time bash x.sh 
0 files removed
yum update pid: 33233
Docker CE Stable - x86_64                                                                                                                                                            0.0  B/s |   0  B     00:05    
Error: Failed to download metadata for repo 'docker-ce-stable': Librepo was interrupted by a signal

real    0m5.605s
user    0m0.507s
sys 0m0.106s

So the difference for single tests is 0.043s 😄 that is less than 1% and scripts are slightly different.

If you could give an example of a program that is "stopped/killed" slowly after running with sudo versus running as root without sudo I will be happy to investigate.

Best, Alex

millert commented 6 months ago

@AlexBaranowski Thanks for testing. I'm closing this as it doesn't appear to be an issue with sudo.