Skip to content

Commit

Permalink
Merge pull request #431 from hercules-ci/fix-handshake-timeout
Browse files Browse the repository at this point in the history
Fix handshake timeout
  • Loading branch information
roberth authored May 17, 2022
2 parents d9fdd93 + dc77632 commit d7cf6b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ logger logSettings_ storeProtocolVersionValue entriesSource = do
dropMiddle :: MonadIO m => ConduitM (Flush LogEntry) (Flush LogEntry) m ()
dropMiddle = do
-- rich logging
takeCWhileStopEarly isChunk richLogLimit
_ <- takeCWhileStopEarly isChunk richLogLimit
-- degrade to text logging (in case rich logging produces excessive non-textual data)
visibleLinesOnly .| withMessageLimit isChunk textOnlyLogLimit tailLimit snipStart snip snipped

Expand Down
33 changes: 16 additions & 17 deletions hercules-ci-agent/src/Hercules/Agent/Socket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import Network.URI (URI, uriAuthority, uriPath, uriPort, uriQuery, uriRegName, u
import Network.WebSockets (Connection, runClientWith)
import qualified Network.WebSockets as WS
import Protolude hiding (atomically, handle, race, race_)
import UnliftIO.Async (AsyncCancelled (AsyncCancelled), race, race_)
import qualified UnliftIO
import UnliftIO.Async (race, race_)
import UnliftIO.Exception (handle)
import UnliftIO.STM (readTVarIO)
import UnliftIO.Timeout (timeout)
Expand Down Expand Up @@ -225,31 +226,29 @@ runReliableSocket socketConfig writeQueue serviceMessageChan highestAcked = kati
pass
else noAckCleanupThread' expectedN
forever do
removeTimeout <- prepareTimeout handshakeTimeoutMicroseconds HandshakeTimeout
handle logWarningPause $
withConnection' socketConfig $
\conn -> do
katipAddNamespace "Handshake" do
handshake conn removeTimeout
readThread conn `race_` writeThread conn `race_` noAckCleanupThread
withCancelableTimeout handshakeTimeoutMicroseconds HandshakeTimeout \removeTimeout -> do
withConnection' socketConfig $
\conn -> do
katipAddNamespace "Handshake" do
handshake conn removeTimeout
readThread conn `race_` writeThread conn `race_` noAckCleanupThread

handshakeTimeoutMicroseconds :: Int
handshakeTimeoutMicroseconds = 30_000_000

data HandshakeTimeout = HandshakeTimeout
deriving (Show, Exception)

prepareTimeout :: (Exception e, MonadIO m) => Int -> e -> m (IO ())
prepareTimeout delay exc = do
withCancelableTimeout :: (Exception e, MonadUnliftIO m) => Int -> e -> (IO () -> m a) -> m a
withCancelableTimeout delay exc cont = do
requestingThread <- liftIO myThreadId
tid <- liftIO $ forkIO do
do
threadDelay delay
throwTo requestingThread exc
`catch` \(_ :: AsyncCancelled) ->
-- Removal of the timeout is normal, so do nothing
pass
pure $ throwTo tid AsyncCancelled
UnliftIO.withAsync
( liftIO do
threadDelay delay
throwTo requestingThread exc
)
(cont . cancel)

msgN :: Frame o a -> Maybe Integer
msgN Frame.Msg {n = n} = Just n
Expand Down

0 comments on commit d7cf6b7

Please sign in to comment.