Incorrect blocked on MVar detection
Given the code:
main = do
v <- newEmptyMVar
forkFinally (putStrLn $ let x = x in x) (putMVar v)
print =<< takeMVar v
The spawned thread should raise a
NonTermination exception (assuming idle GC), then put it in the MVar
v, which should cause the main thread to abort printing
However, in reality, the main thread aborts with
Exception: thread blocked indefinitely in an MVar operation.
My debugging shows that the spawned thread raises
NonTermination and at the same time the main thread raises
BlockedIndefinitelyOnMVar. Of course, the main thread is not really blocked on the exception, and after the finally bit runs, the MVar is filled. Adding tracing shows that the
putMVar is called.
Hypothesis is that the GC notices it can raise the non-termination exception, and then opportunistically excludes that thread from further GC scanning, and detects the MVar is unreferenced, raising the subsequent exception. Of course, given you can catch the exception, that isn't safe. Roman Leshchinskiy worries that in the wrong circumstances the MVar might really be being GC'd and thus could corrupt memory. This issue was detected while debugging an error in the Shake build system for GHC itself.