Mystro256 4dfb518
commit dabd0e900f6e10c358436c584c51dd1f55c320d2
Mystro256 4dfb518
Author: Thomas Koch <linrunner@gmx.net>
Mystro256 4dfb518
Date:   Sun Mar 12 20:53:32 2017 +0100
Mystro256 4dfb518
Mystro256 4dfb518
    Issue #242: mitigate slow shutdown
Mystro256 4dfb518
    
Mystro256 4dfb518
    Rationale: 'systemctl stop tlp' invokes 'nmcli radio on <device>' in systemd
Mystro256 4dfb518
    context. This causes nmcli to hang for ~20 sec after switching the device.
Mystro256 4dfb518
    The cause is unknown – when invoking nmcli directly it doesn't hang.
Mystro256 4dfb518
    
Mystro256 4dfb518
    Workaround:
Mystro256 4dfb518
    * Execute radio switching last in 'tlp init [start|stop]'
Mystro256 4dfb518
    * Add TimeoutStopSec=3 to tlp.service so systemd will kill the hanging
Mystro256 4dfb518
      stop task after 3 secs.
Mystro256 4dfb518
    
Mystro256 4dfb518
    Caveat: does not cover all cases yet. When two radios (e.g. wifi and
Mystro256 4dfb518
    wwan) are to be switched, the second switch never happens.
Mystro256 4dfb518
Mystro256 4dfb518
diff --git a/tlp.in b/tlp.in
Mystro256 4dfb518
index bc2f852..fd0486c 100644
Mystro256 4dfb518
--- a/tlp.in
Mystro256 4dfb518
+++ b/tlp.in
Mystro256 4dfb518
@@ -96,23 +96,21 @@ case "$mode" in
Mystro256 4dfb518
         # do init business ...
Mystro256 4dfb518
         case $mode2 in
Mystro256 4dfb518
             start)
Mystro256 4dfb518
-                # apply radio states
Mystro256 4dfb518
-                set_radio_device_states start
Mystro256 4dfb518
-
Mystro256 4dfb518
-                # apply power save settings -- but only if not previously run
Mystro256 4dfb518
-                # (by the udev rule) for the same power state
Mystro256 4dfb518
-                if compare_and_save_power_state $pwrmode; then
Mystro256 4dfb518
-                    echo -n "Applying power save settings..."
Mystro256 4dfb518
-                    apply_common_settings $pwrmode
Mystro256 4dfb518
-                    [ "$pwrmode" = "1" ] && poweroff_drivebay 0
Mystro256 4dfb518
-                    [ "$X_TLP_USB_MODE" = "1" ] && set_usb_suspend 0 auto
Mystro256 4dfb518
-                    echo "done."
Mystro256 4dfb518
-                fi
Mystro256 4dfb518
+                # apply power save settings
Mystro256 4dfb518
+                compare_and_save_power_state $pwrmode
Mystro256 4dfb518
+                echo -n "Applying power save settings..."
Mystro256 4dfb518
+                apply_common_settings $pwrmode
Mystro256 4dfb518
+                poweroff_drivebay $pwrmode 0
Mystro256 4dfb518
+                [ "$X_TLP_USB_MODE" = "1" ] && set_usb_suspend 0 auto
Mystro256 4dfb518
+                echo "done."
Mystro256 4dfb518
 
Mystro256 4dfb518
                 # apply battery settings
Mystro256 4dfb518
                 echo -n "Setting battery charge thresholds..."
Mystro256 4dfb518
                 set_charge_thresholds
Mystro256 4dfb518
                 echo "done."
Mystro256 4dfb518
+
Mystro256 4dfb518
+                # apply radio states
Mystro256 4dfb518
+                set_radio_device_states start
Mystro256 4dfb518
                 ;;
Mystro256 4dfb518
 
Mystro256 4dfb518
             restart|force-reload)
Mystro256 4dfb518
@@ -131,14 +129,6 @@ case "$mode" in
Mystro256 4dfb518
                 ;;
Mystro256 4dfb518
 
Mystro256 4dfb518
             stop)
Mystro256 4dfb518
-                set_radio_device_states stop
Mystro256 4dfb518
-
Mystro256 4dfb518
-                if [ "$USB_AUTOSUSPEND_DISABLE_ON_SHUTDOWN" = "1" ]; then
Mystro256 4dfb518
-                    echo -n "Disabling usb autosuspend..."
Mystro256 4dfb518
-                    set_usb_suspend 0 on
Mystro256 4dfb518
-                    echo "done."
Mystro256 4dfb518
-                fi
Mystro256 4dfb518
-
Mystro256 4dfb518
                 # remove usb startup flag
Mystro256 4dfb518
                 [ -f $USB_DONE ] && rm $USB_DONE
Mystro256 4dfb518
 
Mystro256 4dfb518
@@ -149,6 +139,16 @@ case "$mode" in
Mystro256 4dfb518
                 echo -n "Applying power save settings..."
Mystro256 4dfb518
                 apply_common_settings 0
Mystro256 4dfb518
                 echo "done."
Mystro256 4dfb518
+
Mystro256 4dfb518
+                # disable usb autosuspend if configured
Mystro256 4dfb518
+                if [ "$USB_AUTOSUSPEND_DISABLE_ON_SHUTDOWN" = "1" ]; then
Mystro256 4dfb518
+                    echo -n "Disabling usb autosuspend..."
Mystro256 4dfb518
+                    set_usb_suspend 0 on
Mystro256 4dfb518
+                    echo "done."
Mystro256 4dfb518
+                fi
Mystro256 4dfb518
+
Mystro256 4dfb518
+                # apply radio states
Mystro256 4dfb518
+                set_radio_device_states stop
Mystro256 4dfb518
                 ;;
Mystro256 4dfb518
 
Mystro256 4dfb518
             *)
Mystro256 4dfb518
diff --git a/tlp.service.in b/tlp.service.in
Mystro256 4dfb518
index 7dc4b2c..67c131c 100644
Mystro256 4dfb518
--- a/tlp.service.in
Mystro256 4dfb518
+++ b/tlp.service.in
Mystro256 4dfb518
@@ -15,6 +15,7 @@ Type=oneshot
Mystro256 4dfb518
 RemainAfterExit=yes
Mystro256 4dfb518
 ExecStart=@TLP_SBIN@/tlp init start
Mystro256 4dfb518
 ExecStop=@TLP_SBIN@/tlp init stop
Mystro256 4dfb518
+TimeoutStopSec=3
Mystro256 4dfb518
 
Mystro256 4dfb518
 [Install]
Mystro256 4dfb518
 WantedBy=multi-user.target