Skip to content

Commit 10673e6

Browse files
committed
[fix] evaluate key in lookupKeyValue to avoid reordering with newKey
1 parent 8cac5fb commit 10673e6

File tree

1 file changed

+9
-1
lines changed
  • hls-graph/src/Development/IDE/Graph/Internal

1 file changed

+9
-1
lines changed

hls-graph/src/Development/IDE/Graph/Internal/Key.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module Development.IDE.Graph.Internal.Key
3434
) where
3535

3636
--import Control.Monad.IO.Class ()
37+
import Control.Exception (evaluate)
3738
import Data.Coerce
3839
import Data.Dynamic
3940
import qualified Data.HashMap.Strict as Map
@@ -85,8 +86,15 @@ newKey k = unsafePerformIO $ do
8586

8687
lookupKeyValue :: Key -> KeyValue
8788
lookupKeyValue (UnsafeMkKey x) = unsafePerformIO $ do
89+
-- NOTE:
90+
-- The reason for this evaluate is that the x, if not forced yet, is a thunk
91+
-- that forces the atomicModifyIORef' in the creation of the new key. If it
92+
-- isn't forced *before* reading the keyMap, the keyMap will only obtain the new
93+
-- key (x) *after* the IntMap is already copied out of the keyMap reference,
94+
-- i.e. when it is forced for the lookup in the IntMap.
95+
k <- evaluate x
8896
GlobalKeyValueMap _ im _ <- readIORef keyMap
89-
pure $! im IM.! x
97+
pure $! im IM.! k
9098

9199
{-# NOINLINE lookupKeyValue #-}
92100

0 commit comments

Comments
 (0)