Skip to content

Rethrow exceptions from forked threads in bye #478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions tls/Network/TLS/Core.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_HADDOCK hide #-}

module Network.TLS.Core (
Expand Down Expand Up @@ -98,7 +99,8 @@ getRTT ctx = do
--
-- this doesn't actually close the handle
bye :: MonadIO m => Context -> m ()
bye ctx = liftIO $ E.handle swallowSync $ do
bye ctx = liftIO $ do
tid <- myThreadId
eof <- ctxEOF ctx
tls13 <- tls13orLater ctx
when (tls13 && not eof) $ do
Expand All @@ -112,8 +114,10 @@ bye ctx = liftIO $ E.handle swallowSync $ do
unless recvNST $ do
rtt <- getRTT ctx
var <- newEmptyMVar
_ <- forkIOWithUnmask $ \umask -> E.handle swallowSync $
umask (void $ timeout rtt $ recvHS13 ctx chk) `E.finally` putMVar var ()
_ <- forkIOWithUnmask $ \umask ->
E.handle @E.SomeException (E.throwTo tid) $
umask (void $ timeout rtt $ recvHS13 ctx chk)
`E.finally` putMVar var ()
takeMVar var
else do
-- receiving Client Finished
Expand All @@ -124,18 +128,12 @@ bye ctx = liftIO $ E.handle swallowSync $ do
-- fixme: 1sec is good enough?
let rtt = 1000000
var <- newEmptyMVar
_ <- forkIOWithUnmask $ \umask -> E.handle swallowSync $
umask (void $ timeout rtt $ recvHS13 ctx chk) `E.finally` putMVar var ()
_ <- forkIOWithUnmask $ \umask ->
E.handle @E.SomeException (E.throwTo tid) $
umask (void $ timeout rtt $ recvHS13 ctx chk)
`E.finally` putMVar var ()
takeMVar var
bye_ ctx
where
-- Swallow synchronous exceptions, rethrow asynchronous exceptions
swallowSync :: E.SomeException -> IO ()
swallowSync e
| Just (E.SomeAsyncException ae) <- E.fromException e
= E.throwIO ae
| otherwise
= return ()

bye_ :: MonadIO m => Context -> m ()
bye_ ctx = liftIO $ do
Expand Down
Loading