[Git][ghc/ghc][wip/romes/async-eval-resume] 6 commits: fixup! loader: fix #28104
Rodrigo Mesquita pushed to branch wip/romes/async-eval-resume at Glasgow Haskell Compiler / GHC Commits: d3d013a5 by Andrea Vezzosi at 2026-11-27T10:34:04+01:00 fixup! loader: fix #28104 - - - - - 5f654f2c by Andrea Vezzosi at 2026-11-27T14:08:26+01:00 fixup! loader: fix #28104 - - - - - 92181450 by Rodrigo Mesquita at 2026-11-30T11:50:34+00:00 Global break action which uses a per-thread context map Previously, everytime an expression was run in the interpreter (`sandboxIO`): 1) the RTS global variable `rts_breakpoint_io_action`, which is run anytime any thread hits a breakpoint, got set to a new action that: a) Wrote the breakpoint hit by the thread to the `statusMVar` b) Blocked waiting on a newly created MVar `breakMVar` for a `resumeStmt` to signal it. 2) Start the expression evaluation on a new "sandbox" thread 3) Block waiting on `statusMVar` for a breakpoint to be hit, and we'd return the read `EvalBreak` 4) Set the `rts_breakpoint_io_action` to a no-op which prints "Ignored breakpoint..." At some later point, we could `resumeStmt` with the `EvalBreak` context by signaling the `breakMVar` on which the thread that hit the breakpoint is blocked. However, if you consider an expression which forks off threads which hit breakpoints, surprising things may happen: main = do -- the `worker` code has a breakpoint mapM (forkIO . worker) [1..10] forever $ threadDelay 10000000 Interpreting this program, we'll do (1,2,3), and all workers will hit their breakpoint and run `rts_breakpoint_io_action`. Here's a first issue: 1. The first worker succeeds in `putMVar statusMVar` 2. The interpreter will read `takeMVar statusMVar` (3) and then (4) 3. The remaining workers are hitting the breakpoint concurrently: - Some may block trying to `putMVar statusMVar` - Some may run the no-op action which discards the breakpoint Depending on whether the remaining workers hit the breakpoint or (4) happens first, some workers will skip their breakpoint. Now, let's pretend all workers can block on `putMVar statusMVar`. Since the interpreter will take the breakpoints one by one, and resume them one by one, it seemingly works. However, that's only fine when we assume only one expression is evaluated at once. As soon as multiple expressions can be evaluated at once (a desireable property of a debugger, as well as something that can already happen today in some scenarios (see `break049`)), it becomes madness: The second issue comes the global break action being swapped by a second evaluation and threads from the first evaluation running the "wrong" action. Consider: 1. eval `expr_A` installs a break action that puts on `statusMVar_A` 2. `expr_A` forks off a few threads, they're running in the background 3. eval `expr_B` sets the break action to put on `statusMVar_B` 4. the `expr_A` forked threads hit a breakpoint and put `statusMVar_B` 5. `expr_B` returns with a breakpoint of `expr_A`, `resume expr_B` would resume `expr_A`, etc... A third issue with having a single MVar for all threads of an execution is that there is no way to have fine-grained control over which threads are resumed and which are stopped. This is often necessary to debug multi-threaded programs too. Therefore, it is imperative that the global break action does not capture a global single MVar when many expressions with many corresponding threads can be running in the interpreter/debugger at once, if we want to support debugging multi-threaded programs at all. --- This commit fixes these foundational issues by making the global break action truly global (i.e. never modified and never capturing any locals). The global break action is installed once in `sandboxIO` and never removed (`withBreakAciton` and `noBreakAction` were killed). Subsequent runs of `sandboxIO` re-rewrite the same break action (a no-op). When any thread hits the global break action, it looks its `ThreadId` up in the global per-thread context mapping, returning a `ResumeContext` which contains that thread's `breakMVar` (now `resumeBreakVar`) and `statusMVar` (now `resumeEvalStatus`). Each thread blocks on its own MVar and replies on its own TVar. There is no longer one global status nor break MVar. To resume a thread by its `ThreadId`, we can simply fetch its `ResumeContext` and signal the corresponding `resumeBreakMVar` To wait for a result or for a breakpoint in a specific thread, we do `readThreadEvalStatus`. To wait for any breakpoint hit in any thread at all, we have `readAnyThreadEvalBreak`. Some evaluations will only care to look at their own result and breaks (so they run with `readAllBreaks` `= False`); and the main thread of an evaluation should not be read by other evaluations. In short, only threads forked off by the main thread of evaluations get read with `readAnyThreadEvalBreak`. If two evaluations fork off threads in the background, `readAnyThreadEvalBreak` may return threads from any of them. There is no just way to distinguish their "parents" at runtime. For instance, `compileParsedExprRemote` should run an expression until completion without any existing threads interfering with its execution, so it runs with `readAllBreaks = False`. Otherwise, by default, running `main` on the REPL, we'll want to stop at any break any forked thread hits, just like we did before. P.S. Currently, we need to keep track of the "main expression thread" being currently evaluated to fetch its result, but, when `evalStmt` becomes asynchronous by returning the `ThreadId` of the "sandbox" thread, the whole `evaluationMainThreadGlobalVar` hack can be deleted. --- New tests observing behavior that we don't want to break (which I broke at some point while developing this patch): - break032: we don't stop at breakpoints in TH - break033: :continue after -fbreak-on-exception doesn't hit exception breakpoint - break034: test :abandon doesn't hang with -fbreak-on-error - conc033: also run with the external interpreter - ForceInterrupt(Ext): ^C during :force interrupts the forced expression - break043: :continue of a forked thread reads main thread breaks/results too - break046: evaluating expr while stopped doesn't change what :continue resumes - break048: evaluating expr while forked thread is stopped doesn't hit again current breakpoint New tests that failed before this commit: - T3171(ext)nosandbox: T3171 with -fno-ghci-sandbox (T3171extnosandbox still fails) - break050(ext): ^C during :continue of a forked thread interrupts that thread Fixed ext-interp bc. resumeStmt now does unsafeUnmask, just like sandboxIO, so the UserInterrupt reaches the resume. - break049: breakpoints in forked threads don't get mixed up with other evaluations This test demonstrates one way multi-threaded debugging can go wrong bc the global break action used to capture a break MVar for a specific thread that is shared by all threads (described above as "the second issue"). Here is the annotated execution of the test *before this commit*: ```haskell ghci> main Stopped in Main.inMain, break049.hs:30:12-18 _result :: IO () = _ [break049.hs:30:12-18] ghci> :force slow -- ANN: the breakpoint we're ignoring on :force is the worker's -- breakpoint, and it's just discarded *** Ignoring breakpoint [break049.hs:27:14-20] slow = 5 [break049.hs:30:12-18] ghci> :cmd return "" -- ANN: we :continue `main`, but the worker should still be blocked -- so `main` should block. [break049.hs:30:12-18] ghci> :continue -- ANN: however, it completes; recall the worker break was ignored -- instead of stopping there main done -- ANN: after this commit, the worker break wouldn't have been read -- by `:force`, and we'd see it when resuming `:main`, and -- `:continue` here would resume the worker and only then main would -- complete. ghci> :continue -- ANN: bc this is ghci before the commit, we see: not stopped at a breakpoint ghci> ``` - break042: main forks workers and all workers hit a breakpoint This test witnesses "the first issue" described above, where the workers hit breaks after the global break action was already removed and so are lost. Fixes #28124 - - - - - 5c677445 by Rodrigo Mesquita at 2026-11-30T11:50:34+00:00 disable breakpoints in forked threads to run TH in ext interp and other merge artifacts that do not come from conflict TODO: fix up to either the async rpc or to the global break changes, depending on who lands first the new tests pass; and we even observed that break032 fails without disableThreadBreakpoints - - - - - 5d02b548 by Rodrigo Mesquita at 2026-11-30T11:50:34+00:00 test: Run ghci037 with -fexternal-interpreter too Observes a previously untested bug in evaluation with -fno-ghci-sandbox which was temporarily introduced in the next commit. - - - - - e0963f9f by Rodrigo Mesquita at 2026-11-30T17:43:37+00:00 Asynchrounous interpreter eval/resume interface from GHC After #28124 (global break action) and #27960 (asynchronous ext interpreter RPC), we can adapt the interpreter evalStmt and resumeStmt functions from GHC.Runtime.Interpreter to be more asynchronous in the sense that we don't need to block immediately waiting for the evaluation result, or for a resumed evaluation result. While GHCi won't necessarily benefit from this immediately (though it could, with some work on the interface), it allows API consumers to tap into the multi-threaded debugging capabilities, resuming threads independently from waiting from them. Implementation wise: - The evalStmt and resumeStmt `interpCmd` wrappers in GHC no longer return the result synchronously; and there's a new waitThreadResult command to await the result of a started or resumed thread - The execStmt and resumeExec GHC API functions, which call `evalStmt` and `resumeStmt` under the hood remain synchronous, to not break the likely users of these high-level evaluate-in-interpreter functions. - The more subtle change is that we can no longer wrap the synchronous execution of an evaluation in `redirectInterrupts`, expecting ^Cs thrown to the interpreter to be redirected to the thread being evaluated and waited for. We introduce `redirectInterrupts` in the GHC side, which wraps a call to `waitThreadResult`, and throws the exception to the interpreter with a new ThrowToThread command. The redirect interrupt details are in the new Note [interpreter redirectInterrupts and signal handling] Notes: - The async eval/resume design requires an interpreter sandbox ThreadId to be held by the host program. Therefore, we won't be able to detect a deadlock in a sandboxed thread and won't send a BlockedIndefinitely, so conc033 won't work on the ghci way. This is acceptable because BlockedIndefinitely is a debug mechanism, and the RTS doesn't guarantee that it is thrown. Fixes #28228 - - - - - 67 changed files: - + changelog.d/28124 - + changelog.d/28228 - compiler/GHC/Driver/Config.hs - compiler/GHC/Runtime/Eval.hs - compiler/GHC/Runtime/Eval/Types.hs - compiler/GHC/Runtime/Interpreter.hs - compiler/GHC/Tc/Gen/Splice.hs - ghc/GHCi/UI/Monad.hs - libraries/ghci/GHCi/Message.hs - libraries/ghci/GHCi/Run.hs - + libraries/ghci/GHCi/Run/Breakpoints.hs - libraries/ghci/GHCi/Server.hs - libraries/ghci/GHCi/Signals.hs - libraries/ghci/ghci.cabal.in - rts/Interpreter.c - testsuite/tests/concurrent/should_run/all.T - testsuite/tests/ghc-api/T8628.hs - testsuite/tests/ghc-api/T8639_api.hs - testsuite/tests/ghc-api/apirecomp001/myghc.hs - testsuite/tests/ghci.debugger/scripts/Makefile - testsuite/tests/ghci.debugger/scripts/T14690.stdout - testsuite/tests/ghci.debugger/scripts/all.T - testsuite/tests/ghci.debugger/scripts/break024.stdout - + testsuite/tests/ghci.debugger/scripts/break032.hs - + testsuite/tests/ghci.debugger/scripts/break032.script - + testsuite/tests/ghci.debugger/scripts/break032.stdout - + testsuite/tests/ghci.debugger/scripts/break033.hs - + testsuite/tests/ghci.debugger/scripts/break033.script - + testsuite/tests/ghci.debugger/scripts/break033.stdout - + testsuite/tests/ghci.debugger/scripts/break034.hs - + testsuite/tests/ghci.debugger/scripts/break034.script - + testsuite/tests/ghci.debugger/scripts/break034.stdout - testsuite/tests/ghci.debugger/scripts/break037.hs - testsuite/tests/ghci.debugger/scripts/break037.script - testsuite/tests/ghci.debugger/scripts/break037.stdout - + testsuite/tests/ghci.debugger/scripts/break042.hs - + testsuite/tests/ghci.debugger/scripts/break042.script - + testsuite/tests/ghci.debugger/scripts/break042.stderr - + testsuite/tests/ghci.debugger/scripts/break042.stdout - + testsuite/tests/ghci.debugger/scripts/break043.hs - + testsuite/tests/ghci.debugger/scripts/break043.script - + testsuite/tests/ghci.debugger/scripts/break043.stdout - + testsuite/tests/ghci.debugger/scripts/break046.hs - + testsuite/tests/ghci.debugger/scripts/break046.script - + testsuite/tests/ghci.debugger/scripts/break046.stdout - + testsuite/tests/ghci.debugger/scripts/break048.hs - + testsuite/tests/ghci.debugger/scripts/break048.script - + testsuite/tests/ghci.debugger/scripts/break048.stdout - + testsuite/tests/ghci.debugger/scripts/break049.hs - + testsuite/tests/ghci.debugger/scripts/break049.script - + testsuite/tests/ghci.debugger/scripts/break049.stdout - + testsuite/tests/ghci.debugger/scripts/break050.hs - + testsuite/tests/ghci.debugger/scripts/break050.script - + testsuite/tests/ghci.debugger/scripts/break050.stdout - + testsuite/tests/ghci.debugger/scripts/break050ext.stdout - testsuite/tests/ghci/interp-cancellation/Main.hs - testsuite/tests/ghci/linking/all.T - testsuite/tests/ghci/scripts/Makefile - testsuite/tests/ghci/scripts/all.T - + testsuite/tests/ghci/scripts/ghci037ext.stdout - + testsuite/tests/ghci/should_run/ForceInterrupt.script - + testsuite/tests/ghci/should_run/ForceInterrupt.stdout - + testsuite/tests/ghci/should_run/ForceInterruptExt.stdout - testsuite/tests/ghci/should_run/Makefile - + testsuite/tests/ghci/should_run/T3171extnosandbox.stdout - + testsuite/tests/ghci/should_run/T3171nosandbox.stdout - testsuite/tests/ghci/should_run/all.T The diff was not included because it is too large. View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/a7f092c07e2ba52870274afc1268b27... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/a7f092c07e2ba52870274afc1268b27... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
participants (1)
-
Rodrigo Mesquita (@alt-romes)