Opened 2 years ago

Closed 2 years ago

#5611 closed bug (fixed)

Asynchronous exception discarded after safe FFI call

Reported by: joeyadams Owned by: simonmar
Priority: normal Milestone: 7.2.1
Component: Runtime System Version: 7.0.3
Keywords: FFI, exception Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Incorrect result at runtime Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

Note: This bug appears to be fixed already, as it does not appear with GHC 7.2.1 . I'm submitting a bug report anyway, to document its presence.

The bug is: when an asynchronous exception is thrown to a thread making a (safe) foreign call, the thread throwing the exception blocks like it should, but then the exception isn't actually delivered. For example:

{-# LANGUAGE ForeignFunctionInterface #-}

import Control.Concurrent
import Foreign.C
import System.IO

foreign import ccall safe "unistd.h sleep"
    sleep :: CUInt -> IO CUInt

main :: IO ()
main = do
    hSetBuffering stdout LineBuffering

    tid <- forkIO $ do
        putStrLn "child: Sleeping"
        _ <- sleep 1

        -- The following lines should not happen after the killThread from the
        -- parent thread completes.  However, they do...
        putStrLn "child: Done sleeping"
        threadDelay 1000000
        putStrLn "child: Done waiting"

    threadDelay 100000
    putStrLn $ "parent: Throwing exception to thread " ++ show tid
    throwTo tid $ userError "Exception delivered successfully"
    putStrLn "parent: Done throwing exception"

    threadDelay 2000000

When the bug is present, the program prints:

child: Sleeping
parent: Throwing exception to thread ThreadId 4
child: Done sleeping
parent: Done throwing exception
child: Done waiting

"child: Done waiting" should not be printed after completion of the throwTo, and the exception message should appear. On GHC 7.2.1, the program prints:

child: Sleeping
parent: Throwing exception to thread ThreadId 4
parent: Done throwing exception
ffi-sleep: user error (Exception delivered successfully)

This bug has been reproduced in GHC 7.0.3, on both Linux and Windows. It has also been reproduced on GHC 7.0.4.

Change History (2)

comment:1 Changed 2 years ago by simonmar

  • Owner set to simonmar

Thanks for the report.

This appears to have been fixed by commit 539d3adec64f51a3fb13bb65b7a494d7eded01a0, though the log message doesn't give any clues about whether it was diagnosed or not. I'll add your program as a test case.

comment:2 Changed 2 years ago by simonmar

  • Milestone set to 7.2.1
  • Resolution set to fixed
  • Status changed from new to closed

resolving as fixed in 7.2.1

Note: See TracTickets for help on using tickets.