Teo Camarasu pushed to branch wip/T28171 at Glasgow Haskell Compiler / GHC Commits: c9ac9ddb by Teo Camarasu at 2026-11-26T10:38:37+00:00 rts/nonmoving: fix wakeBlockingQueue missing update remembered set push When we wake up a blocking queue, we replace the blocking queue with an indirection. An indirection has fewer fields than a blocking queue, so we are, in essence, overwriting some fields with NULL. Thus they need to be pushed to the update remembered set. The test shows that this is observable. We can have a situation where a thread is ONLY being kept alive by being on a blocking queue. If the nonmoving mark can't see it there then it will be GC'd overly eagerly and we will get a NonTermination exception when it gets cleaned up. We also push the other fields to the update remembered set to enforce the invariant, but I'm not sure if those will ever cause issues in the same way. Resolves #28171 - - - - - 5 changed files: - + changelog.d/T28171 - rts/Threads.c - + testsuite/tests/rts/T28171.hs - + testsuite/tests/rts/T28171.stdout - testsuite/tests/rts/all.T Changes: ===================================== changelog.d/T28171 ===================================== @@ -0,0 +1,8 @@ +section: rts +synopsis: Fix the nonmoving GC sending spurious NonTermination exceptions +description: + We fix the nonmoving GC's logic for waking up threads blocked on a thunk, which had + a bug leading to threads being falsely being GC'd too soon. + This could lead to spurious NonTermination exceptions. +mrs: !17050 +issues: #28171 ===================================== rts/Threads.c ===================================== @@ -440,9 +440,19 @@ wakeBlockingQueue(Capability *cap, StgBlockingQueue *bq) if (i != &stg_IND_info) { ASSERT(i == &stg_MSG_BLACKHOLE_info); tryWakeupThread(cap,msg->tso); + } } + // Changing the closure type to IND makes these fields inaccessible, + // so we need to push them to the update remembered set. + // See Note [Update remembered set] in rts/sm/NonMovingMark.c + IF_NONMOVING_WRITE_BARRIER_ENABLED { + updateRemembSetPushClosure(cap, (StgClosure *) bq->bh); + updateRemembSetPushClosure(cap, (StgClosure *) bq->owner); + updateRemembSetPushClosure(cap, (StgClosure *) bq->queue); + } + // overwrite the BQ with an indirection so it will be // collected at the next GC. OVERWRITE_INFO(bq, &stg_IND_info); ===================================== testsuite/tests/rts/T28171.hs ===================================== @@ -0,0 +1,32 @@ +import Control.Concurrent +import Control.Concurrent.MVar +import Control.Exception +import Control.Monad +import System.Mem +import System.IO.Unsafe + +main :: IO () +main = do + forM_ [1 .. 1000] $ \r -> do + -- Create a thunk that will never finish evaluating + let t = unsafePerformIO (threadDelay (100000000000 + r)) + -- Begin nonmoving GC + performMajorGC + performMajorGC + x <- newEmptyMVar + y <- newEmptyMVar + -- we start evaluating it on two threads in parallel + k <- forkIO (putMVar x () >> void (evaluate t)) + performMajorGC + -- The second thread gets added to the waking queue + _ <- forkIO (putMVar y () >> void (evaluate t)) + takeMVar x + takeMVar y + -- We kill the original thread. + -- And now wake up the other threads. + -- We need to ensure that the nonmoving GC can see that thread. + -- Otherwise we will get a NonTermination exception + killThread k + performMajorGC + performMajorGC + putStrLn "done" ===================================== testsuite/tests/rts/T28171.stdout ===================================== @@ -0,0 +1 @@ +done ===================================== testsuite/tests/rts/all.T ===================================== @@ -807,3 +807,9 @@ test('T27376', only_ways(['nonmoving_thr', 'nonmoving_thr_sanity']), extra_run_opts('+RTS -A2m -RTS') ], compile_and_run, ['-O2']) + +test('T28171', + [ req_target_smp, + extra_ways(['nonmoving_thr', 'nonmoving_thr_sanity']), + only_ways(['nonmoving_thr', 'nonmoving_thr_sanity'])], + compile_and_run, ['']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c9ac9ddb655b3146dff9ec86873e07d7... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c9ac9ddb655b3146dff9ec86873e07d7... 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)
-
Teo Camarasu (@teo)