haskell / core-libraries-committee

95 stars 16 forks source link

Attach exception backtrace in `GHC.Conc.throwSTM` #297

Open bgamari opened 1 month ago

bgamari commented 1 month ago

The throwSTM combinator exposed by GHC.Conc, GHC.Conc.Sync, and the stm package currently does not capture and attach a Backtraces annotation to the thrown exception. This functionality was requested in GHC #25365.

I propose that throwSTM gain a HasCallStack constraint and its body be adjusted to attach the appropriate annotation:

-throwSTM :: Exception e => e -> STM a
-throwSTM e = STM $ raiseIO# (toException e)
+throwSTM :: HasCallStack => Exception e => e -> STM a
+throwSTM e = do
+    -- N.B. Typically use of unsafeIOToSTM is very much frowned upon as this
+    -- is an easy way to end up with nested transactions. However, we can be
+    -- certain that toExceptionWithBacktrace will not initiate a transaction.
+    se <- unsafeIOToSTM (toExceptionWithBacktrace e)
+    STM $ raiseIO# se

Implemented in GHC !13408.

bgamari commented 1 day ago

@Bodigrim, just a gentle ping on this and #298.