[GHC] #13457: unsafeInterleaveST is too unsafe

GHC ghc-devs at haskell.org
Mon Mar 20 06:18:17 UTC 2017


#13457: unsafeInterleaveST is too unsafe
-------------------------------------+-------------------------------------
           Reporter:  dfeuer         |             Owner:  (none)
               Type:  bug            |            Status:  new
           Priority:  normal         |         Milestone:  8.4.1
          Component:  Core           |           Version:  8.1
  Libraries                          |
           Keywords:                 |  Operating System:  Unknown/Multiple
       Architecture:                 |   Type of failure:  Incorrect result
  Unknown/Multiple                   |  at runtime
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 Several years ago, Edward Kmett wrote a
 [https://www.schoolofhaskell.com/school/to-infinity-and-beyond/older-but-
 still-interesting/deamortized-st#newsflash--unsafeinterleavest-is-unsafe-
 blog post] that (among other things) explains that `unsafeInterleaveST` is
 not thread-safe; actions may be performed multiple times.

 The fix that would seem to match everything else best would be to rename
 `unsafeInterleaveST` to `unsafeDupableInterleaveST` and define
 `unsafeInterleaveST m = unsafeDupableInterleaveST (noDuplicate >> m)`
 using an `ST`ish version of `noDuplicate`.

 As Kmett explains, the dupable version is useful for high performance when
 the suspended action is known to be idempotent. Actually, the action being
 idempotent is insufficient, but I believe a sufficient condition is that
 the action does not both read and write any reference or array element.

 While we're at it, we need to document all the functions in
 `Control.Monad.ST.Unsafe`.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/13457>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list