Skip to content

Commit edc9532

Browse files
committed
Don't catch async exceptions
Related: haskell/os-string#22
1 parent e794e73 commit edc9532

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

System/FilePath/Internal.hs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ import Data.List(stripPrefix, isSuffixOf, uncons, dropWhileEnd)
129129
#define STRING String
130130
#define FILEPATH FilePath
131131
#else
132-
import Prelude (fromIntegral)
133-
import Control.Exception ( SomeException, evaluate, try, displayException )
132+
import Prelude (fromIntegral, return, IO, Either(..))
133+
import Control.Exception ( catch, displayException, evaluate, fromException, toException, throwIO, Exception, SomeAsyncException(..), SomeException )
134134
import Control.DeepSeq (force)
135135
import GHC.IO (unsafePerformIO)
136136
import qualified Data.Char as C
@@ -1270,15 +1270,31 @@ snoc :: String -> Char -> String
12701270
snoc str = \c -> str <> [c]
12711271

12721272
#else
1273+
-- | Like 'try', but rethrows async exceptions.
1274+
trySafe :: Exception e => IO a -> IO (Either e a)
1275+
trySafe ioA = catch action eHandler
1276+
where
1277+
action = do
1278+
v <- ioA
1279+
return (Right v)
1280+
eHandler e
1281+
| isAsyncException e = throwIO e
1282+
| otherwise = return (Left e)
1283+
1284+
isAsyncException :: Exception e => e -> Bool
1285+
isAsyncException e =
1286+
case fromException (toException e) of
1287+
Just (SomeAsyncException _) -> True
1288+
Nothing -> False
12731289
#ifdef WINDOWS
12741290
fromString :: P.String -> STRING
12751291
fromString str = P.either (P.error . P.show) P.id $ unsafePerformIO $ do
1276-
r <- try @SomeException $ GHC.withCStringLen (mkUTF16le ErrorOnCodingFailure) str $ \cstr -> packCStringLen cstr
1292+
r <- trySafe @SomeException $ GHC.withCStringLen (mkUTF16le ErrorOnCodingFailure) str $ \cstr -> packCStringLen cstr
12771293
evaluate $ force $ first displayException r
12781294
#else
12791295
fromString :: P.String -> STRING
12801296
fromString str = P.either (P.error . P.show) P.id $ unsafePerformIO $ do
1281-
r <- try @SomeException $ GHC.withCStringLen (mkUTF8 ErrorOnCodingFailure) str $ \cstr -> packCStringLen cstr
1297+
r <- trySafe @SomeException $ GHC.withCStringLen (mkUTF8 ErrorOnCodingFailure) str $ \cstr -> packCStringLen cstr
12821298
evaluate $ force $ first displayException r
12831299
#endif
12841300

0 commit comments

Comments
 (0)