Bug 77416 - systemctl poweroff/reboot does not bring down user services respectfully
Summary: systemctl poweroff/reboot does not bring down user services respectfully
Status: RESOLVED NOTABUG
Alias: None
Product: systemd
Classification: Unclassified
Component: general (show other bugs)
Version: unspecified
Hardware: x86-64 (AMD64) Linux (All)
: medium normal
Assignee: systemd-bugs
QA Contact: systemd-bugs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-04-14 05:09 UTC by vimregisters
Modified: 2014-05-24 07:36 UTC (History)
0 users

See Also:
i915 platform:
i915 features:


Attachments

Description vimregisters 2014-04-14 05:09:46 UTC
I have a .service file that goes something like

[Unit]
Description=Google Chrome Browser
After=startx.service

[Service]
ExecStart=/usr/bin/google-chrome
KillMode=process
KillSignal=SIGTERM

When I do 'systemctl --user stop/restart chrome' or 'systemctl --user default'  systemd performs perfectly and the expected happens, chrome exits cleanly or restarts cleanly.

However, when I do something like 'systemctl reboot', the order in which the services are brought down is respected (i.e. chrome and pekwm before X) but the method of bringing the services down is not. After a reboot, chrome whines about being improperly shutdown, and sometimes even has a lock on the profile preventing chrome from opening again.

I suspect this is a systemd problem since using any of the commands like --user isolate/default/stop/restart etc, it works perfectly, but it all goes to crap when I do systemctl reboot
Comment 1 vimregisters 2014-04-20 11:03:20 UTC
I went and studied some units in /usr/lib/systemd/system so I can get a better idea of how things are supposed to be done. I saw a pattern, so I mimicked it with good results.

For example, network.target will pull in a bunch of things as dependencies, but each one of those units will also contain a "Before=network.target" directive, to make sure that network.target is the "marker" that signifies all network things are brought up.

So I arranged my user services to x11.target pulling in startx.service
Then I have "Before=x11.target" in startx.service

Then in chrome.service, I have "After=x11.target"

This seems to fix my problem. It's strange since the original problem wasn't that things were killed out-of-order.. journalctl --user -b -1 shows that all user services are killed in the right order, just that chrome wasn't being killed in the way I specified.

Is this expected behavior? Why didn't just doing After=startx.service do the trick?



BTW, I reread my previous comment. The [Unit] I had posted wasn't a copy/paste, so I left out some details.. I had a Requires= directive to trigger deactivation. This is a copy/paste I found from a snapshot of my filesystem.

[Unit]
Description=Launch Chrome Browser
Requires=startx.service x11.target
After=startx.service

[Service]
KillMode=process
ExecStart=/usr/bin/google-chrome-unstable

TL;DR
I changed startx.service to have "Before=x11.target" and I changed chrome.service to have "After=x11.target" instead of "After=startx.service" and now it works as expected
Comment 2 Lennart Poettering 2014-05-24 06:56:41 UTC
The X11 libs kill processes hard if the connection is severed. THis is most likely what you experienced, and is out of systemd's hands. If you order things properly against X11 however, and make sure that X11 is terminated only after chrome then the X11 libs will not see the connectiosn terminated, and thus is the correct fix for the issue. Closing hence.
Comment 3 vimregisters 2014-05-24 07:36:39 UTC
(In reply to comment #2)
> The X11 libs kill processes hard if the connection is severed. THis is most
> likely what you experienced, and is out of systemd's hands. If you order
> things properly against X11 however, and make sure that X11 is terminated
> only after chrome then the X11 libs will not see the connectiosn terminated,
> and thus is the correct fix for the issue. Closing hence.

It is kind of strange though. It now mostly works,
but still sometimes fails. I spoke too soon in my
last comment.

There's lots of strangeness going on with my
setup. I'm sure I have things ordered correctly,
and things mostly work, but occasionally things
are as they were before, and there are
occassional startup problems.

I'll do a 'systemctl --user start graphical.target
+some_other_gui_stuff' and X isn't started
correctly, but the GUI stuff are running and
active according to systemctl, which makes no
sense.

But back to the original problem


I've read the systemd documentation and looked
through some unit files in /usr/lib~, so I'm pretty
confident I have the dependency stuff figured out
(enough). 

X has:
Wants=x11.target
Before=x11.target
WantedBy=graphical.target

GUI stuff (including chrome) have:
Requires=x11.target
After=x11.target

So the startup order should be:
X
x11.target
gui stuff

Then at shutdown or deactivation, the Before and
After are reversed, so the units deactivate in
this order:
gui stuff
x11.target
X

The order should be correct, so Chrome shouldn't
be killed by X since X wouldn't even think about
shutting down until Chrome is gone. Is it because
Chrome takes too long to deactivate? Because it
_does_ spend a good while writing a bunch of stuff
to disk. I'm hesitant to add any sleeps or
timeouts to fix my startup/shutdown issues. If
there are any flaws in my Before/After config,
someone please let me know.


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.