Skip to content

Commit

Permalink
X.H.StatusBar: More robust killPid
Browse files Browse the repository at this point in the history
xmobar sometimes (very rarely though) refuses to die from a single
SIGTERM. This commit implements a more robust killing algorithm that
tries SIGTERM several times and falls back to SIGKILL after one second.

(Implemented for the threaded RTS only.)
  • Loading branch information
liskin committed Jun 4, 2021
1 parent 3bf7d51 commit 80918a1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
20 changes: 17 additions & 3 deletions XMonad/Hooks/StatusBar.hs
Expand Up @@ -57,12 +57,15 @@ module XMonad.Hooks.StatusBar (
killAllStatusBars,
) where

import Control.Concurrent (threadDelay, rtsSupportsBoundThreads)
import Control.Concurrent.Async (race_, mapConcurrently_)
import Control.Exception (SomeException, try)
import Data.IORef (newIORef, readIORef, writeIORef)
import qualified Codec.Binary.UTF8.String as UTF8 (encode)
import qualified Data.Map as M
import System.IO (hClose)
import System.Posix.Signals (sigTERM, signalProcessGroup)
import System.Posix.Process (getGroupProcessStatus)
import System.Posix.Signals (sigTERM, sigKILL, signalProcessGroup)
import System.Posix.Types (ProcessID)

import Foreign.C (CChar)
Expand Down Expand Up @@ -533,7 +536,15 @@ killStatusBar cmd = do
XS.modify (StatusBarPIDs . M.delete cmd . getPIDs)

killPid :: ProcessID -> IO ()
killPid pidToKill = void $ try @SomeException (signalProcessGroup sigTERM pidToKill)
killPid pidToKill | rtsSupportsBoundThreads = try_ $ race_ (killer 50000) waiter
| otherwise = try_ $ signalProcessGroup sigTERM pidToKill
where
try_ = void . try @SomeException
waiter = getGroupProcessStatus True False pidToKill >> waiter
killer delay = do
signalProcessGroup (if delay < 1000000 then sigTERM else sigKILL) pidToKill
threadDelay delay
killer (delay * 2)

-- | Spawns a status bar and saves its PID together with the commands that was
-- used to start it. This is useful when the status bars should be restarted
Expand All @@ -551,4 +562,7 @@ spawnStatusBar cmd = do
-- caveats in 'cleanupStatusBar'
killAllStatusBars :: X ()
killAllStatusBars =
XS.gets (M.elems . getPIDs) >>= io . traverse_ killPid >> XS.put (StatusBarPIDs mempty)
XS.gets (M.elems . getPIDs) >>= io . mapC_ killPid >> XS.put (StatusBarPIDs mempty)
where
mapC_ | rtsSupportsBoundThreads = mapConcurrently_
| otherwise = traverse_
1 change: 1 addition & 0 deletions xmonad-contrib.cabal
Expand Up @@ -57,6 +57,7 @@ library
random,
mtl >= 1 && < 3,
unix,
async,
X11 >= 1.10 && < 1.11,
xmonad >= 0.16.999 && < 0.18,
utf8-string
Expand Down

0 comments on commit 80918a1

Please sign in to comment.