[GHC] #15312: getChanContents exception behavior seems a bit odd

#15312: getChanContents exception behavior seems a bit odd -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: 8.6.1 Component: Core | Version: 8.4.3 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: -------------------------------------+------------------------------------- I've been playing around with `Control.Concurrent.Chan` today. Something seems a bit off: {{{#!hs -- Bug.hs import Control.Concurrent (forkIO, yield) import Control.Concurrent.Chan import Data.List (elem) import Control.Exception import Control.Concurrent.MVar data Ex = Ex deriving Show instance Exception Ex main = do ch <- newChan sync1 <- newEmptyMVar sync2 <- newEmptyMVar forkIO $ do {writeList2Chan ch [1..3*10^6 :: Int]; putMVar sync1 ()} yield writeChan ch (-12) cont <- getChanContents ch tid <- forkIO $ do evaluate (last cont) putMVar sync2 () yield throwTo tid Ex print (elem (3*10^6) cont) takeMVar sync1 tryTakeMVar sync2 }}} When I run this single-threaded (`+RTS -N1`), it prints {{{ Bug: Ex Bug: Ex }}} One of the thunks in the lazy list gets overwritten by the (asynchronous) exception. This seems a bit surprising; is it the way it should be? Does `hGetContents` do this too? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15312 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15312: getChanContents exception behavior seems a bit odd -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: 8.6.1 Component: Core Libraries | Version: 8.4.3 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by dfeuer): The basic problem seems to be that `readChan` uses `modifyMVar`, which uses `onException`, which catches arbitrary exceptions and rethrows them as synchronous exceptions: {{{#!hs onException :: IO a -> IO b -> IO a onException io what = io `catch` \e -> do _ <- what throwIO (e :: SomeException) }}} I don't think `readChan` can throw any synchronous exceptions, so perhaps it should rethrow any exceptions it receives asynchronously and retry. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15312#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15312: getChanContents exception behavior seems a bit odd -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: 8.6.1 Component: Core Libraries | Version: 8.4.3 Resolution: | 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: | -------------------------------------+------------------------------------- Comment (by simonmar): Yeah, this is tricky stuff. We have special handling for async exceptions in the IO library, see `Note [async]` in `GHC.IO.Handle.Internals`. Note that since you threw `Ex` as an async exception, it should really be a child of `AsyncException` in the exception hierarchy, because we use that assumption when handling async exceptions differently. (it wouldn't have made a difference with your code though). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15312#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC