File: DebugLocks.hs

package info (click to toggle)
git-annex 10.20250416-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 73,572 kB
  • sloc: haskell: 90,656; javascript: 9,103; sh: 1,469; makefile: 211; perl: 137; ansic: 44
file content (43 lines) | stat: -rw-r--r-- 1,144 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{- Pinpointing location of MVar/STM deadlocks
 -
 - Copyright 2018 Joey Hess <id@joeyh.name>
 -
 - License: BSD-2-clause
 -}

{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Utility.DebugLocks (debugLocks) where

import Control.Monad.Catch
import Control.Monad.IO.Class
#ifdef DEBUGLOCKS
import Control.Exception (BlockedIndefinitelyOnSTM, BlockedIndefinitelyOnMVar)
import GHC.Stack
import System.IO
#endif

{- Wrap around any action, and if it dies due to deadlock, will display
 - a call stack on stderr when DEBUGLOCKS is defined.
 -
 - Should be zero cost to call when DEBUGLOCKS is not defined.
 -}
#ifdef DEBUGLOCKS
debugLocks :: HasCallStack => (MonadCatch m, MonadIO m) => m a -> m a
debugLocks a = a `catches`
	[ Handler (\ (e :: BlockedIndefinitelyOnMVar) -> go "MVar" e callStack)
	, Handler (\ (e :: BlockedIndefinitelyOnSTM) -> go "STM" e callStack)
	]
  where
	go ty e cs = do
		liftIO $ do
			hPutStrLn stderr $ 
				ty ++ " deadlock detected " ++ prettyCallStack cs
			hFlush stderr
		throwM e
#else
-- No HasCallStack constraint.
debugLocks :: (MonadCatch m, MonadIO m) => m a -> m a
debugLocks a = a
#endif