Teo Camarasu pushed to branch wip/T28171 at Glasgow Haskell Compiler / GHC

Commits:

5 changed files:

Changes:

  • changelog.d/T28171
    1
    +section: rts
    
    2
    +synopsis: Fix the nonmoving GC sending spurious NonTermination exceptions
    
    3
    +description:
    
    4
    +  We fix the nonmoving GC's logic for waking up threads blocked on a thunk, which had
    
    5
    +  a bug leading to threads being falsely being GC'd too soon.
    
    6
    +  This could lead to spurious NonTermination exceptions.
    
    7
    +mrs: !17050
    
    8
    +issues: #28171

  • rts/Threads.c
    ... ... @@ -440,9 +440,18 @@ wakeBlockingQueue(Capability *cap, StgBlockingQueue *bq)
    440 440
             if (i != &stg_IND_info) {
    
    441 441
                 ASSERT(i == &stg_MSG_BLACKHOLE_info);
    
    442 442
                 tryWakeupThread(cap,msg->tso);
    
    443
    +
    
    443 444
             }
    
    444 445
         }
    
    445 446
     
    
    447
    +    // Changing the closure type to IND makes these fields inaccessible,
    
    448
    +    // so we need to push them to the update remembered set.
    
    449
    +    IF_NONMOVING_WRITE_BARRIER_ENABLED {
    
    450
    +      updateRemembSetPushClosure(cap, (StgClosure *) bq->bh);
    
    451
    +      updateRemembSetPushClosure(cap, (StgClosure *) bq->owner);
    
    452
    +      updateRemembSetPushClosure(cap, (StgClosure *) bq->queue);
    
    453
    +    }
    
    454
    +
    
    446 455
         // overwrite the BQ with an indirection so it will be
    
    447 456
         // collected at the next GC.
    
    448 457
         OVERWRITE_INFO(bq, &stg_IND_info);
    

  • testsuite/tests/rts/T28171.hs
    1
    +import Control.Concurrent
    
    2
    +import Control.Concurrent.MVar
    
    3
    +import Control.Exception
    
    4
    +import Control.Monad
    
    5
    +import System.Mem
    
    6
    +import System.IO.Unsafe
    
    7
    +
    
    8
    +main :: IO ()
    
    9
    +main = do
    
    10
    +  forM_ [1 .. 1000] $ \r -> do
    
    11
    +    -- Create a thunk that will never finish evaluating
    
    12
    +    let t = unsafePerformIO (threadDelay (100000000000 + r))
    
    13
    +    -- Begin nonmoving GC
    
    14
    +    performMajorGC
    
    15
    +    performMajorGC
    
    16
    +    x <- newEmptyMVar
    
    17
    +    y <- newEmptyMVar
    
    18
    +    -- we start evaluating it on two threads in parallel
    
    19
    +    k <- forkIO (putMVar x () >> void (evaluate t))
    
    20
    +    performMajorGC
    
    21
    +    -- The second thread gets added to the waking queue
    
    22
    +    _ <- forkIO (putMVar y () >> void (evaluate t))
    
    23
    +    takeMVar x
    
    24
    +    takeMVar y
    
    25
    +    -- We kill the original thread.
    
    26
    +    -- And now wake up the other threads.
    
    27
    +    -- We need to ensure that the nonmoving GC can see that thread.
    
    28
    +    -- Otherwise we will get a NonTermination exception
    
    29
    +    killThread k
    
    30
    +    performMajorGC
    
    31
    +    performMajorGC
    
    32
    +  putStrLn "done"

  • testsuite/tests/rts/T28171.stdout
    1
    +done

  • testsuite/tests/rts/all.T
    ... ... @@ -807,3 +807,9 @@ test('T27376',
    807 807
            only_ways(['nonmoving_thr', 'nonmoving_thr_sanity']),
    
    808 808
            extra_run_opts('+RTS -A2m -RTS') ],
    
    809 809
          compile_and_run, ['-O2'])
    
    810
    +
    
    811
    +test('T28171',
    
    812
    +     [ req_target_smp,
    
    813
    +       extra_ways(['nonmoving_thr', 'nonmoving_thr_sanity']),
    
    814
    +       only_ways(['nonmoving_thr', 'nonmoving_thr_sanity'])],
    
    815
    +     compile_and_run, [''])