From 6af43ab4b6e4bffa8cbcdddf5b8814279a917f28 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Tue, 20 Jun 2023 06:57:51 -0400
Subject: [PATCH] rts: Fix data race in Interpreter's preemption check

---
 rts/Interpreter.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/rts/Interpreter.c b/rts/Interpreter.c
index 1be9cf832cb8..f032e577b9d3 100644
--- a/rts/Interpreter.c
+++ b/rts/Interpreter.c
@@ -315,8 +315,9 @@ interpretBCO (Capability* cap)
 
     LOAD_THREAD_STATE();
 
-    cap->r.rHpLim = (P_)1; // HpLim is the context-switch flag; when it
-                           // goes to zero we must return to the scheduler.
+    // N.B. HpLim is the context-switch flag; when it
+    // goes to zero we must return to the scheduler.
+    RELAXED_STORE_ALWAYS(&cap->r.rHpLim, (P_)1);
 
     IF_DEBUG(interpreter,
              debugBelch(
@@ -1967,7 +1968,7 @@ run_BCO:
             // context switching: sometimes the scheduler can invoke
             // the interpreter with context_switch == 1, particularly
             // if the -C0 flag has been given on the cmd line.
-            if (cap->r.rHpLim == NULL) {
+            if (RELAXED_LOAD(&cap->r.rHpLim) == NULL) {
                 Sp_subW(1); SpW(0) = (W_)&stg_enter_info;
                 RETURN_TO_SCHEDULER(ThreadInterpret, ThreadYielding);
             }
-- 
GitLab