Duncan Coutts pushed to branch wip/io-manager-deadlock-detection at Glasgow Haskell Compiler / GHC
Commits:
-
49fe2278
by Duncan Coutts at 2026-11-24T21:26:33+00:00
-
f8e9fb69
by Duncan Coutts at 2026-11-24T21:26:33+00:00
15 changed files:
- + changelog.d/idle-gc-and-deadlock-detection
- docs/users_guide/profiling.rst
- docs/users_guide/runtime_control.rst
- testsuite/tests/rts/Makefile
- + testsuite/tests/rts/T26408a.hs
- + testsuite/tests/rts/T26408a.stderr
- + testsuite/tests/rts/T26408b.hs
- + testsuite/tests/rts/T26408b.stderr
- + testsuite/tests/rts/T26408c.hs
- + testsuite/tests/rts/T26408c.stdout
- + testsuite/tests/rts/T26408d.hs
- + testsuite/tests/rts/T26408d.stderr
- + testsuite/tests/rts/T26408e.hs
- + testsuite/tests/rts/T26408e.stderr
- testsuite/tests/rts/all.T
Changes:
| 1 | +section: rts
|
|
| 2 | +issues: #27040 #26408
|
|
| 3 | +mrs: !14820
|
|
| 4 | +synopsis: Idle GC and deadlock detection in the non-threaded RTS now behave the same as in the threaded RTS
|
|
| 5 | +description: {
|
|
| 6 | +Idle GC now works in the non-threaded RTS and is enabled by default.
|
|
| 7 | + |
|
| 8 | +Deadlock detection in the non-threaded RTS now follows the same design and
|
|
| 9 | +behaviour as in the threaded RTS. In particular, it can detect partial
|
|
| 10 | +deadlocks where some but not all threads are deadlocked. The detection is no
|
|
| 11 | +longer prompt however, as the detection runs as part of idle GC, which (by
|
|
| 12 | +default) is performed 0.3 seconds after all capabilities become idle.
|
|
| 13 | + |
|
| 14 | +The user guide now documents that deadlock detection relies on idle GC,
|
|
| 15 | +and thus turning off idle GC disables deadlock detection. This is true
|
|
| 16 | +of the threaded RTS in previous GHC releases but it was not previously
|
|
| 17 | +documented behaviour.
|
|
| 18 | + |
|
| 19 | +Internally, the idle GC and deadlock detection now follow the same design and
|
|
| 20 | +use 99% the same code between the non-threaded and threaded RTS. Having a
|
|
| 21 | +single design improves the comprehension and maintainability of the RTS code.
|
|
| 22 | +} |
| ... | ... | @@ -655,11 +655,13 @@ enclosed between ``+RTS ... -RTS`` as usual): |
| 655 | 655 | order to increase the resolution of the time profiler.
|
| 656 | 656 | |
| 657 | 657 | Using a value of zero disables the RTS clock completely, and has the
|
| 658 | - effect of disabling timers that depend on it: the context switch
|
|
| 659 | - timer and the heap profiling timer. Context switches will still
|
|
| 660 | - happen, but deterministically and at a rate much faster than normal.
|
|
| 661 | - Disabling the interval timer is useful for debugging, because it
|
|
| 662 | - eliminates a source of non-determinism at runtime.
|
|
| 658 | + effect of disabling the timers and features that depend on it: the context
|
|
| 659 | + switch timer, the heap profiling timer, the eventlog flush timer, and the
|
|
| 660 | + idle GC timer. Context switches will still happen, but deterministically
|
|
| 661 | + and at a rate much faster than normal. Disabling the interval timer is
|
|
| 662 | + useful for debugging, because it eliminates a source of non-determinism at
|
|
| 663 | + runtime. Note however that disabling the idle GC timer not only disables
|
|
| 664 | + idle GC but also deadlock detection (see rts-flag:`-I`).
|
|
| 663 | 665 | |
| 664 | 666 | |
| 665 | 667 | .. rts-flag:: -xc
|
| ... | ... | @@ -735,61 +735,6 @@ performance. |
| 735 | 735 | ``-H0`` resets both the size suggestion, and dynamic scaling enablied by a
|
| 736 | 736 | bare ``-H``.
|
| 737 | 737 | |
| 738 | -.. rts-flag:: -I ⟨seconds⟩
|
|
| 739 | - |
|
| 740 | - :default: 0.3 seconds in the threaded runtime, 0 in the non-threaded runtime
|
|
| 741 | - |
|
| 742 | - .. index::
|
|
| 743 | - single: idle GC
|
|
| 744 | - |
|
| 745 | - Set the amount of idle time which must pass before a idle GC is
|
|
| 746 | - performed. Setting ``-I0`` disables the idle GC.
|
|
| 747 | - |
|
| 748 | - In the threaded and SMP versions of the RTS (see :ghc-flag:`-threaded`,
|
|
| 749 | - :ref:`options-linker`), a major GC is automatically performed if the
|
|
| 750 | - runtime has been idle (no Haskell computation has been running) for a
|
|
| 751 | - period of time.
|
|
| 752 | - |
|
| 753 | - For an interactive application, it is probably a good idea to use
|
|
| 754 | - the idle GC, because this will allow finalizers to run and
|
|
| 755 | - deadlocked threads to be detected in the idle time when no Haskell
|
|
| 756 | - computation is happening. Also, it will mean that a GC is less
|
|
| 757 | - likely to happen when the application is busy, and so responsiveness
|
|
| 758 | - may be improved. However, if the amount of live data in the heap is
|
|
| 759 | - particularly large, then the idle GC can cause a significant delay,
|
|
| 760 | - and too small an interval could adversely affect interactive
|
|
| 761 | - responsiveness.
|
|
| 762 | - |
|
| 763 | - The idle period timer only resets after some activity
|
|
| 764 | - by a Haskell thread. If your program is doing literally nothing then
|
|
| 765 | - after the first idle collection is triggered then no more future collections
|
|
| 766 | - will be scheduled until more work is performed.
|
|
| 767 | - |
|
| 768 | - This is an experimental feature, please let us know if it causes
|
|
| 769 | - problems and/or could benefit from further tuning.
|
|
| 770 | - |
|
| 771 | -.. rts-flag:: -Iw ⟨seconds⟩
|
|
| 772 | - |
|
| 773 | - :default: 0 seconds
|
|
| 774 | - |
|
| 775 | - .. index::
|
|
| 776 | - single: idle GC
|
|
| 777 | - |
|
| 778 | - Set the minimum wait time between runs of the idle GC.
|
|
| 779 | - |
|
| 780 | - By default, if idle GC is enabled in the threaded runtime, a major
|
|
| 781 | - GC will be performed every time the process goes idle for a
|
|
| 782 | - sufficiently long duration (see :rts-flag:`-I ⟨seconds⟩`). For
|
|
| 783 | - large server processes accepting regular but infrequent requests
|
|
| 784 | - (e.g., once per second), an expensive, major GC may run after
|
|
| 785 | - every request. As an alternative to shutting off idle GC entirely
|
|
| 786 | - (with ``-I0``), a minimum wait time between idle GCs can be
|
|
| 787 | - specified with this flag. For example, ``-Iw60`` will ensure that
|
|
| 788 | - an idle GC runs at most once per minute.
|
|
| 789 | - |
|
| 790 | - This is an experimental feature, please let us know if it causes
|
|
| 791 | - problems and/or could benefit from further tuning.
|
|
| 792 | - |
|
| 793 | 738 | .. rts-flag:: -ki ⟨size⟩
|
| 794 | 739 | |
| 795 | 740 | :default: 1k
|
| ... | ... | @@ -1015,6 +960,118 @@ performance. |
| 1015 | 960 | ``GHC.Stats.getRTSStats`` from Haskell.
|
| 1016 | 961 | |
| 1017 | 962 | |
| 963 | +Idle GC and deadlock detection
|
|
| 964 | +------------------------------
|
|
| 965 | + |
|
| 966 | +The RTS performs idle GC and deadlock detection. These features are more
|
|
| 967 | +closely related and dependent than one might expect. In particular, deadlock
|
|
| 968 | +detection relies on idle GC.
|
|
| 969 | + |
|
| 970 | +The primary purpose of idle GC is to reclaim memory and return it to the system
|
|
| 971 | +when the application has been idle for a short time. It also gives an
|
|
| 972 | +opportunity for finalisers to run more promptly. See :rts-flag:`-I ⟨seconds⟩`.
|
|
| 973 | + |
|
| 974 | +Deadlock detection is a debugging feature to try to discover when some or all
|
|
| 975 | +Haskell threads are deadlocked. For the purpose of this explanation we define:
|
|
| 976 | + |
|
| 977 | + * a "partial deadlock" to be a set of threads that are deadlocked; and
|
|
| 978 | + * a "system deadlock" is when all threads are deadlocked.
|
|
| 979 | + |
|
| 980 | +The deadlock detection is implemented by the GC, and it is capable of detecting
|
|
| 981 | +partial deadlocks. If a deadlock is discovered, the RTS will throw an
|
|
| 982 | +asynchronous exception to (some or all of) the blocked threads to enable
|
|
| 983 | +progress and enable better reporting of the problem. In particular it throws
|
|
| 984 | +the async exceptions ``BlockedIndefinitelyOnMVar`` or
|
|
| 985 | +``BlockedIndefinitelyOnSTM``, as appropriate.
|
|
| 986 | + |
|
| 987 | +The deadlock detection can be run during a major GC, but it is more expensive
|
|
| 988 | +than a normal major GC so it is not run during normal major GCs. The deadlock
|
|
| 989 | +detection mode of GC is run by the idle GC. This is on the basis that when the
|
|
| 990 | +application is idle is when we are most likely to find a deadlock, and also
|
|
| 991 | +that the extra time cost is unlikely to be a problem because the application is
|
|
| 992 | +already idle.
|
|
| 993 | + |
|
| 994 | +There are a few consequence of this:
|
|
| 995 | + |
|
| 996 | + * Deadlock detection relies on idle GC and will not run if idle GC is disabled
|
|
| 997 | + with ``-I0``.
|
|
| 998 | + * Deadlock detection will not run if the application remains busy, and thus
|
|
| 999 | + idle GC is not invoked.
|
|
| 1000 | + * Deadlock detection is not prompt: it will only occur after the idle GC
|
|
| 1001 | + delay, depending on both :rts-flag:`-I ⟨seconds⟩` and
|
|
| 1002 | + :rts-flag:`-Iw ⟨seconds⟩`.
|
|
| 1003 | + |
|
| 1004 | +In the non-threaded RTS there is additional behaviour: system deadlocks will be
|
|
| 1005 | +detected promptly, and this works even if idle GC is otherwise disabled.
|
|
| 1006 | +(This is not supported by the WinIO I/O manager however, see GHC issue #28057).
|
|
| 1007 | + |
|
| 1008 | +One cannot rely on deadlock detection as part of the normal program behaviour.
|
|
| 1009 | +It should only be used for debugging. Firstly, deadlock detection can be
|
|
| 1010 | +disabled or indefinately deferred due to activity. Secondly, the set of threads
|
|
| 1011 | +that deadlock detection throws exceptions to is not minimal: a thread waiting
|
|
| 1012 | +on a deadlocked thread is also considered deadlocked and will have an exception
|
|
| 1013 | +thrown to it even if it could make progress after the deadlocked thread is
|
|
| 1014 | +terminated.
|
|
| 1015 | + |
|
| 1016 | +Deadlock detection can be invoked manually using ``performDeadlockDetection``
|
|
| 1017 | +from ``System.Mem.Experimental``.
|
|
| 1018 | + |
|
| 1019 | + |
|
| 1020 | +.. rts-flag:: -I ⟨seconds⟩
|
|
| 1021 | + |
|
| 1022 | + :default: 0.3 seconds
|
|
| 1023 | + |
|
| 1024 | + .. index::
|
|
| 1025 | + single: idle GC
|
|
| 1026 | + |
|
| 1027 | + A major GC is automatically performed if the runtime has been idle (no
|
|
| 1028 | + Haskell computation has been running) for a period of time. Set the amount
|
|
| 1029 | + of idle time which must pass before a idle GC is performed.
|
|
| 1030 | + |
|
| 1031 | + Setting ``-I0`` disables the idle GC. This also has the unfortunate side
|
|
| 1032 | + effect of disabling thread deadlock detection (the implementation of which
|
|
| 1033 | + uses the idle GC).
|
|
| 1034 | + |
|
| 1035 | + For an interactive application, it is probably a good idea to use
|
|
| 1036 | + the idle GC, because this will allow finalizers to run and
|
|
| 1037 | + deadlocked threads to be detected in the idle time when no Haskell
|
|
| 1038 | + computation is happening. Also, it will mean that a GC is less
|
|
| 1039 | + likely to happen when the application is busy, and so responsiveness
|
|
| 1040 | + may be improved. However, if the amount of live data in the heap is
|
|
| 1041 | + particularly large, then the idle GC can cause a significant delay,
|
|
| 1042 | + and too small an interval could adversely affect interactive
|
|
| 1043 | + responsiveness.
|
|
| 1044 | + |
|
| 1045 | + The idle period timer only resets after some activity
|
|
| 1046 | + by a Haskell thread. If your program is doing literally nothing then
|
|
| 1047 | + after the first idle collection is triggered then no more future collections
|
|
| 1048 | + will be scheduled until more work is performed.
|
|
| 1049 | + |
|
| 1050 | + Please let us know if it causes problems and/or could benefit from further
|
|
| 1051 | + tuning.
|
|
| 1052 | + |
|
| 1053 | +.. rts-flag:: -Iw ⟨seconds⟩
|
|
| 1054 | + |
|
| 1055 | + :default: 0 seconds
|
|
| 1056 | + |
|
| 1057 | + .. index::
|
|
| 1058 | + single: idle GC
|
|
| 1059 | + |
|
| 1060 | + Set the minimum wait time between runs of the idle GC.
|
|
| 1061 | + |
|
| 1062 | + By default (and if idle GC is not disabled) a major
|
|
| 1063 | + GC will be performed every time the process goes idle for a
|
|
| 1064 | + sufficiently long duration (see :rts-flag:`-I ⟨seconds⟩`). For
|
|
| 1065 | + large server processes accepting regular but infrequent requests
|
|
| 1066 | + (e.g., once per second), an expensive, major GC may run after
|
|
| 1067 | + every request. As an alternative to shutting off idle GC entirely
|
|
| 1068 | + (with ``-I0``), a minimum wait time between idle GCs can be
|
|
| 1069 | + specified with this flag. For example, ``-Iw60`` will ensure that
|
|
| 1070 | + an idle GC runs at most once per minute.
|
|
| 1071 | + |
|
| 1072 | + This is an experimental feature, please let us know if it causes
|
|
| 1073 | + problems and/or could benefit from further tuning.
|
|
| 1074 | + |
|
| 1018 | 1075 | |
| 1019 | 1076 | .. _rts-options-statistics:
|
| 1020 | 1077 |
| ... | ... | @@ -163,3 +163,11 @@ census_pinned: |
| 163 | 163 | '$(TEST_HC)' $(TEST_HC_OPTS) -O -rtsopts -v0 census_pinned.hs -o census_pinned
|
| 164 | 164 | ./census_pinned +RTS -hT -i0 -RTS
|
| 165 | 165 | grep -m1 '^ARR_WORDS' census_pinned.hp
|
| 166 | + |
|
| 167 | +.PHONY: T26408c
|
|
| 168 | +T26408c:
|
|
| 169 | + "$(TEST_HC)" $(TEST_HC_OPTS) -rtsopts T26408c.hs; \
|
|
| 170 | + ./T26408c +RTS -I0 -T & \
|
|
| 171 | + sleep 1; \
|
|
| 172 | + kill -s USR1 $$!; \
|
|
| 173 | + wait $$! |
| 1 | +import Control.Concurrent
|
|
| 2 | +import Control.Concurrent.STM
|
|
| 3 | +import Control.Exception
|
|
| 4 | +import Control.Monad
|
|
| 5 | + |
|
| 6 | +-- | Test to make sure that we can detect a /partial deadlock/ that is /not/
|
|
| 7 | +-- also a /system deadlock/: that is we can detect a set of deadlocked threads
|
|
| 8 | +-- even when there are other unrelated threads that are waiting on I\/O or
|
|
| 9 | +-- timeouts.
|
|
| 10 | +--
|
|
| 11 | +-- Historically this did not work in the non-threaded RTS which would only do
|
|
| 12 | +-- deadlock detection if there was a system deadlock: no runnable threads /and/
|
|
| 13 | +-- no pending I\/O or timers.
|
|
| 14 | +--
|
|
| 15 | +-- See Note [Deadlock detection]
|
|
| 16 | +--
|
|
| 17 | +main :: IO ()
|
|
| 18 | +main = do
|
|
| 19 | + |
|
| 20 | + -- Set up two threads that are deadlocked on each other
|
|
| 21 | + aDone <- newTVarIO False
|
|
| 22 | + bDone <- newTVarIO False
|
|
| 23 | + let blockingThread theirDone ourDone =
|
|
| 24 | + atomically $ do
|
|
| 25 | + done <- readTVar theirDone
|
|
| 26 | + guard done
|
|
| 27 | + writeTVar ourDone True
|
|
| 28 | + _ <- forkIO (blockingThread bDone aDone)
|
|
| 29 | + _ <- forkIO (blockingThread aDone bDone)
|
|
| 30 | + |
|
| 31 | + -- Set up another thread that is blocked on a long timeout.
|
|
| 32 | + --
|
|
| 33 | + -- We use a timeout rather than I/O as it's more portable, whereas I/O waits
|
|
| 34 | + -- are different between posix and windows I/O managers.
|
|
| 35 | + --
|
|
| 36 | + -- One gotcha is that when the timeout completes then the deadlock will be
|
|
| 37 | + -- detected again (since the bug is about I/O or timeouts masking deadlock
|
|
| 38 | + -- detection). So for a reliable test the timeout used here must be longer
|
|
| 39 | + -- than the test framework's own timeout. So we use maxBound, and we adjust
|
|
| 40 | + -- the test framework's timeout to be short (see run_timeout_multiplier).
|
|
| 41 | + _ <- forkIO (threadDelay maxBound)
|
|
| 42 | + |
|
| 43 | + -- Wait on the two deadlocked threads to terminate. This is now a partial
|
|
| 44 | + -- deadlock but not a system deadlock (the main thread and the first two
|
|
| 45 | + -- threads are deadlocked, but the one waiting on the timeout is ok).
|
|
| 46 | + --
|
|
| 47 | + -- We now expect that the threads that are deadlocked are detected as such
|
|
| 48 | + -- and an exception is thrown to some or all of them.
|
|
| 49 | + --
|
|
| 50 | + -- Note that if this fails, the test itself will effectively deadlock and
|
|
| 51 | + -- will rely on the test framework's timeout.
|
|
| 52 | + atomically $ do
|
|
| 53 | + status <- mapM readTVar [aDone, bDone]
|
|
| 54 | + guard (or status) |
| 1 | +T26408a: Uncaught exception ghc-internal:GHC.Internal.IO.Exception.BlockedIndefinitelyOnSTM:
|
|
| 2 | + |
|
| 3 | +thread blocked indefinitely in an STM transaction |
| 1 | +import Control.Concurrent
|
|
| 2 | +import Control.Concurrent.STM
|
|
| 3 | +import Control.Exception
|
|
| 4 | +import Control.Monad
|
|
| 5 | + |
|
| 6 | +-- | Test to make sure that we can detect a /system deadlock/ promptly which
|
|
| 7 | +-- means without relying on idle GC. To check this we have to turn off idle GC,
|
|
| 8 | +-- (+RTS -I0) which is what normally triggers deadlock detection.
|
|
| 9 | +--
|
|
| 10 | +-- This is only expected to work in the non-threaded RTS. In principle this
|
|
| 11 | +-- test could also work with the threaded RTS when using only a single
|
|
| 12 | +-- capability, if the I\/O managers can support it. Currently that is not the
|
|
| 13 | +-- case.
|
|
| 14 | +--
|
|
| 15 | +-- See Note [Deadlock detection]
|
|
| 16 | +--
|
|
| 17 | +main :: IO ()
|
|
| 18 | +main = do
|
|
| 19 | + |
|
| 20 | + -- Set up two threads that are deadlocked on each other
|
|
| 21 | + aDone <- newTVarIO False
|
|
| 22 | + bDone <- newTVarIO False
|
|
| 23 | + let blockingThread theirDone ourDone =
|
|
| 24 | + atomically $ do
|
|
| 25 | + done <- readTVar theirDone
|
|
| 26 | + guard done
|
|
| 27 | + writeTVar ourDone True
|
|
| 28 | + _ <- forkIO (blockingThread bDone aDone)
|
|
| 29 | + _ <- forkIO (blockingThread aDone bDone)
|
|
| 30 | + |
|
| 31 | + -- Wait on the two deadlocked threads to terminate. This is now a system
|
|
| 32 | + -- deadlock, involving all three threads (the main thread and the other two).
|
|
| 33 | + --
|
|
| 34 | + -- We now expect that the threads that are deadlocked are detected as such
|
|
| 35 | + -- and an exception is thrown to some or all of them.
|
|
| 36 | + --
|
|
| 37 | + -- Note that if this fails, the test itself will effectively deadlock and
|
|
| 38 | + -- will rely on the test framework's timeout.
|
|
| 39 | + atomically $ do
|
|
| 40 | + status <- mapM readTVar [aDone, bDone]
|
|
| 41 | + guard (or status) |
| 1 | +T26408b: Uncaught exception ghc-internal:GHC.Internal.IO.Exception.BlockedIndefinitelyOnSTM:
|
|
| 2 | + |
|
| 3 | +thread blocked indefinitely in an STM transaction |
| 1 | +import Control.Concurrent
|
|
| 2 | +import Control.Concurrent.STM
|
|
| 3 | +import Control.Exception
|
|
| 4 | +import Control.Monad
|
|
| 5 | + |
|
| 6 | +import System.Posix.Signals
|
|
| 7 | +import GHC.Stats.Experimental
|
|
| 8 | + |
|
| 9 | +-- | Test to make sure that we do not falsely detect a deadlock when there is
|
|
| 10 | +-- nothing to do except wait on signals. This is to make sure that our prompt
|
|
| 11 | +-- system deadlock detection is not too eager.
|
|
| 12 | +--
|
|
| 13 | +-- We arrange a partial deadlock that can be broken if a signal arrives and
|
|
| 14 | +-- the signal handler can unblock things.
|
|
| 15 | +--
|
|
| 16 | +-- This should /not/ result in any deadlock being detected, and no other odd
|
|
| 17 | +-- behaviour, like getting caught in a loop and doing too many idle GCs. So we
|
|
| 18 | +-- count and report the number of major GCs.
|
|
| 19 | +--
|
|
| 20 | +main :: IO ()
|
|
| 21 | +main = do
|
|
| 22 | + |
|
| 23 | + -- Set up two threads that are deadlocked on each other
|
|
| 24 | + aDone <- newTVarIO False
|
|
| 25 | + bDone <- newTVarIO False
|
|
| 26 | + let blockingThread theirDone ourDone =
|
|
| 27 | + atomically $ do
|
|
| 28 | + done <- readTVar theirDone
|
|
| 29 | + guard done
|
|
| 30 | + writeTVar ourDone True
|
|
| 31 | + |
|
| 32 | + installHandler userDefinedSignal1
|
|
| 33 | + (Catch $ do
|
|
| 34 | + atomically $ writeTVar aDone True
|
|
| 35 | + >> writeTVar bDone True
|
|
| 36 | + putStrLn "caught SIGUSR1")
|
|
| 37 | + Nothing
|
|
| 38 | + |
|
| 39 | + _ <- forkIO (blockingThread bDone aDone)
|
|
| 40 | + _ <- forkIO (blockingThread aDone bDone)
|
|
| 41 | + yield
|
|
| 42 | + |
|
| 43 | + -- Wait on the deadlocked threads to terminate. We now expect that the threads
|
|
| 44 | + -- that are deadlocked are detected as such and an exception is raised.
|
|
| 45 | + -- Note that if this fails, the test itself will effectively deadlock and
|
|
| 46 | + -- will rely on the test framework's timeout.
|
|
| 47 | + putStrLn "waiting on deadlocked threads..."
|
|
| 48 | + atomically $ do
|
|
| 49 | + status <- mapM readTVar [aDone, bDone]
|
|
| 50 | + guard (or status)
|
|
| 51 | + |
|
| 52 | + -- We expect exactly 1 major GC: the one triggered for deadlock detection
|
|
| 53 | + -- where we find no deadlock. We should then sleep without doing more GC.
|
|
| 54 | + -- An easy mistake would be looping: detecting probable deadlock, do idle GC.
|
|
| 55 | + -- See notifyIdleGcDeadlock().
|
|
| 56 | + stats <- getRTSStats
|
|
| 57 | + putStrLn ("terminated normally, major GCs = " ++ show (major_gcs stats))
|
|
| 58 | + -- This test _could_ be sensitive to changes in default heap sizes etc. If
|
|
| 59 | + -- this number ends up as 2 for example, then adjust the RTS opts (in the
|
|
| 60 | + -- makefile) so that the heap is big enough that normally no major GC would
|
|
| 61 | + -- be needed. |
| 1 | +[1 of 2] Compiling Main ( T26408c.hs, T26408c.o )
|
|
| 2 | +[2 of 2] Linking T26408c
|
|
| 3 | +waiting on deadlocked threads...
|
|
| 4 | +caught SIGUSR1
|
|
| 5 | +terminated normally, major GCs = 1 |
| 1 | +import Control.Concurrent
|
|
| 2 | +import Foreign.C
|
|
| 3 | + |
|
| 4 | +-- | Test to make sure that we detect a system deadlock even when idle GC is
|
|
| 5 | +-- disabled and the system has been idle long enough for the timer tick to be
|
|
| 6 | +-- turned off.
|
|
| 7 | + |
|
| 8 | +main :: IO ()
|
|
| 9 | +main = do
|
|
| 10 | + m <- newEmptyMVar
|
|
| 11 | + _ <- c_sleep 1 -- longer than the 300ms idle GC timeout
|
|
| 12 | + takeMVar m -- should detect system deadlock here
|
|
| 13 | + |
|
| 14 | +foreign import ccall unsafe "unistd.h sleep" c_sleep :: CUInt -> IO CUInt
|
|
| 15 | + |
| 1 | +T26408d: Uncaught exception ghc-internal:GHC.Internal.IO.Exception.BlockedIndefinitelyOnMVar:
|
|
| 2 | + |
|
| 3 | +thread blocked indefinitely in an MVar operation |
| 1 | +import Control.Concurrent
|
|
| 2 | +import Control.Concurrent.STM
|
|
| 3 | +import Control.Exception
|
|
| 4 | +import Control.Monad
|
|
| 5 | +import System.Mem.Experimental (performDeadlockDetection)
|
|
| 6 | + |
|
| 7 | +-- | Test to make sure that we can detect a partial deadlock when idle GC is
|
|
| 8 | +-- turned off, and we manually invoke performDeadlockDetection.
|
|
| 9 | +--
|
|
| 10 | +main :: IO ()
|
|
| 11 | +main = do
|
|
| 12 | + |
|
| 13 | + -- Set up two threads that are deadlocked on each other
|
|
| 14 | + aDone <- newTVarIO False
|
|
| 15 | + bDone <- newTVarIO False
|
|
| 16 | + let blockingThread theirDone ourDone =
|
|
| 17 | + atomically $ do
|
|
| 18 | + done <- readTVar theirDone
|
|
| 19 | + guard done
|
|
| 20 | + writeTVar ourDone True
|
|
| 21 | + _ <- forkIO (blockingThread bDone aDone)
|
|
| 22 | + _ <- forkIO (blockingThread aDone bDone)
|
|
| 23 | + |
|
| 24 | + -- Arrange for another thread to wait a bit (longer than the default 300ms
|
|
| 25 | + -- idle GC timeout, just to be sure) and then manually invoke deadlock
|
|
| 26 | + -- detection.
|
|
| 27 | + _ <- forkIO (threadDelay 500_000 >> performDeadlockDetection)
|
|
| 28 | + |
|
| 29 | + -- And another thread to ensure that we don't have a system deadlock once
|
|
| 30 | + -- the thread above terminates. Otheriwse we might get false test passes
|
|
| 31 | + -- even if performDeadlockDetection were a no-op because when the thread
|
|
| 32 | + -- above terminates then (if not for this one) all threads would be
|
|
| 33 | + -- deadlocked, and system deadlock detection would kick in and detect the
|
|
| 34 | + -- deadlock instead (defeating the point of the test).
|
|
| 35 | + _ <- forkIO (threadDelay maxBound)
|
|
| 36 | + |
|
| 37 | + -- Wait on the two deadlocked threads to terminate. This is now a partial
|
|
| 38 | + -- deadlock but not a system deadlock (the main thread and the first two
|
|
| 39 | + -- threads are deadlocked, but the one waiting on the timeout is ok).
|
|
| 40 | + --
|
|
| 41 | + -- When the manually invoked deadlock detection runs (above) we expect that
|
|
| 42 | + -- the threads that are deadlocked are detected as such, and that an
|
|
| 43 | + -- exception will be thrown to all of them.
|
|
| 44 | + --
|
|
| 45 | + -- Note that if this fails, the test itself will effectively deadlock and
|
|
| 46 | + -- will rely on the test framework's timeout.
|
|
| 47 | + atomically $ do
|
|
| 48 | + status <- mapM readTVar [aDone, bDone]
|
|
| 49 | + guard (or status) |
| 1 | +T26408e: Uncaught exception ghc-internal:GHC.Internal.IO.Exception.BlockedIndefinitelyOnSTM:
|
|
| 2 | + |
|
| 3 | +thread blocked indefinitely in an STM transaction |
| ... | ... | @@ -701,6 +701,35 @@ test('T22859', |
| 701 | 701 | omit_ways(llvm_ways + ['nonmoving_thr', 'nonmoving_thr_sanity'])],
|
| 702 | 702 | compile_and_run, ['-with-rtsopts -A8K'])
|
| 703 | 703 | |
| 704 | +# Deadlock detection testsL
|
|
| 705 | +# A: check we can detect a partial deadlock that is not a system deadlock
|
|
| 706 | +test('T26408a', [exit_code(1), run_timeout_multiplier(0.1)], compile_and_run, [''])
|
|
| 707 | +# B: check we can detect system deadlock with idle GC turned off (+RTS -I0)
|
|
| 708 | +test('T26408b', [all_available_io_managers_except(['winio', 'mio']),
|
|
| 709 | + omit_ways(['threaded2']),
|
|
| 710 | + extra_run_opts('+RTS -I0 -RTS'),
|
|
| 711 | + exit_code(1), run_timeout_multiplier(0.1)],
|
|
| 712 | + compile_and_run, ['-rtsopts'])
|
|
| 713 | +# C: check we do not falsely detect deadlock when blocked only on signals
|
|
| 714 | +# Skip for win32, wasm and js since this test requires posix signals
|
|
| 715 | +test('T26408c', [when(opsys('mingw32'), skip),
|
|
| 716 | + when(arch('wasm32'), skip), js_skip,
|
|
| 717 | + run_timeout_multiplier(0.1)],
|
|
| 718 | + makefile_test, ['T26408c'])
|
|
| 719 | +# D: check we do detect deadlock with idle GC off and a long pause
|
|
| 720 | +test('T26408d', [all_available_io_managers_except(['winio', 'mio']),
|
|
| 721 | + js_skip, omit_ways(['threaded2']),
|
|
| 722 | + extra_run_opts('+RTS -I0 -RTS'),
|
|
| 723 | + exit_code(1), run_timeout_multiplier(0.1)],
|
|
| 724 | + compile_and_run, ['-rtsopts'])
|
|
| 725 | +# E: test manual performDeadlockDetection
|
|
| 726 | +test('T26408e', [extra_run_opts('+RTS -I0 -RTS'),
|
|
| 727 | + exit_code(1), run_timeout_multiplier(0.1)],
|
|
| 728 | + compile_and_run, ['-rtsopts'])
|
|
| 729 | +# TODO: test with I/O managers for threaded ways with +RTS -N1,
|
|
| 730 | +# but it will be broken for now.
|
|
| 731 | + |
|
| 732 | + |
|
| 704 | 733 | # These tests need access to the internal RTS headers.
|
| 705 | 734 | # TODO: there is probably some cleaner way to do this, and it should probably
|
| 706 | 735 | # be guarded for in-tree tests, since it cannot work against an arbitrary
|