[systemd-devel] Kill on console works, ExecStop not

Colin Guthrie colin at mageia.org
Wed Aug 12 08:17:04 PDT 2015


Florian Lindner wrote on 12/08/15 15:47:
> Hello,
> 
> I have a systemd 224 user service unit that starts the emacs daemon:
> 
> [Unit]
> Description=Emacs: the extensible, self-documenting text editor
> 
> [Service]
> Type=forking
> ExecStart=/usr/bin/emacs --daemon
> ExecStop=/usr/bin/emacsclient --eval "(progn (setq kill-emacs-hook nil) 
> (kill-emacs))"
> Restart=always
> 
> [Install]
> WantedBy=default.target
> 
> 
> I starts just fine, but stopping is problematic:
> 
> % systemctl --user start emacs                                                                                                                                                                                           
> % systemctl --user status emacs
> ● emacs.service - Emacs: the extensible, self-documenting text editor
>    Loaded: loaded (/home/florian/.config/systemd/user/emacs.service; 
> disabled; vendor preset: enabled)
>    Active: active (running) since Mi 2015-08-12 15:57:32 CEST; 13s ago
>   Process: 1331 ExecStart=/usr/bin/emacs --daemon (code=exited, 
> status=0/SUCCESS)
>  Main PID: 1332 (emacs)
>    CGroup: /user.slice/user-1000.slice/user at 1000.service/emacs.service
>            ├─1332 /usr/bin/emacs --daemon
>            └─1343 /usr/sbin/idn --quiet --idna-to-ascii --usestd3asciirules
> 
> [...]
> 
> % systemctl --user stop emacs
> % systemctl --user status emacs
> ● emacs.service - Emacs: the extensible, self-documenting text editor
>    Loaded: loaded (/home/florian/.config/systemd/user/emacs.service; 
> disabled; vendor preset: enabled)
>    Active: failed (Result: exit-code) since Mi 2015-08-12 16:03:21 CEST; 6s 
> ago
>   Process: 1906 ExecStop=/usr/bin/emacsclient --eval (progn (setq kill-
> emacs-hook nil) (kill-emacs)) (code=exited, status=0/SUCCESS)
>   Process: 1331 ExecStart=/usr/bin/emacs --daemon (code=exited, 
> status=0/SUCCESS)
>  Main PID: 1332 (code=exited, status=15)
> 
> [...]
> Aug 12 16:03:21 asaru systemd[499]: Stopping Emacs: the extensible, self-
> documenting text editor...
> Aug 12 16:03:21 asaru systemd[499]: emacs.service: Main process exited, 
> code=exited, status=15/n/a
> Aug 12 16:03:21 asaru systemd[499]: Stopped Emacs: the extensible, self-
> documenting text editor.
> Aug 12 16:03:21 asaru systemd[499]: emacs.service: Unit entered failed 
> state.
> Aug 12 16:03:21 asaru systemd[499]: emacs.service: Failed with result 'exit-
> code'.
> 
> 
> However, stopping with the same command works:
> 
> % /usr/bin/emacs --daemon                                                                                                                                                                                               
> [...]
> % /usr/bin/emacsclient --eval "(progn (setq kill-emacs-hook nil) (kill-
> emacs))"
> 
> returns 0 exit code.

Note that the exit code from the emacclient is 0 in both cases.

The systemd status snippet you quote above is complaining that the exit
code for PID 1332 is 15, (i.e. the main process (emacs --daemon)) and it
happily reports that your emacsclient --eval command exited with status 0.

Is it expected that emacs --daemon exits with status 15 when called via
such an eval? If so, you can configure the unit accordingly to tell it
about that exit code as you mentioned already.

There is also the possibility of a race condition here.

It could be that "emacsclient --eval" returns before waiting for the
results of the signal (i.e. it exits after *sending* the signal, but
before the signal is fully handled). In this case, when it's run as part
of an ExecStop, systemd will then move on to a killing spree (it expects
ExecStop to leave things clean, so if anything is left, it mops up),
sending SIGTERM to the emacs --daemon process. Depending on how it
handles the signals and where in it's destruction process (from the
emacsclient signal) it *might* exit with status 15.

HTHs

Col




-- 

Colin Guthrie
colin(at)mageia.org
http://colin.guthr.ie/

Day Job:
  Tribalogic Limited http://www.tribalogic.net/
Open Source:
  Mageia Contributor http://www.mageia.org/
  PulseAudio Hacker http://www.pulseaudio.org/
  Trac Hacker http://trac.edgewall.org/


More information about the systemd-devel mailing list