Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
description: Starting a testnet can no longer hang forever while waiting for a node, as `startNode` polls the node process with `getProcessExitCode` and no longer cancels a thread blocked in `waitForProcess`.
kind:
- bugfix
pr: 6750
project: cardano-testnet
15 changes: 12 additions & 3 deletions cardano-testnet/src/Testnet/Runtime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ import qualified GHC.Stack as GHC
import Network.Socket (HostAddress, PortNumber)
import Prettyprinter (unAnnotate)
import qualified System.Directory as IO
import System.Exit (ExitCode)
import System.FilePath
import qualified System.IO as IO
import qualified System.Process as IO
import System.Process (waitForProcess)
import System.Timeout (timeout)

import Testnet.Filepath
Expand All @@ -68,7 +68,7 @@ import Hedgehog.Extras.Stock.IO.Network.Sprocket (Sprocket (..))
import qualified Hedgehog.Extras.Stock.IO.Network.Sprocket as H
import qualified Hedgehog.Extras.Test.Concurrent as H

import RIO (race, runRIO)
import RIO (race, runRIO, threadDelay)

data NodeStartFailure
= ProcessRelatedFailure ProcessError
Expand Down Expand Up @@ -194,7 +194,7 @@ startNode tp node ipv4 port _testnetMagic mNodeBin nodeCmd = GHC.withFrozenCallS
-- Wait for socket to be created and check for process to not exit
res <- liftIOAnnotated $
race
(waitForProcess hProcess)
(pollForProcessExit hProcess)
( Ping.waitForSprocket
120 -- timeout
0.2 -- check interval
Expand Down Expand Up @@ -414,6 +414,15 @@ closeHandlesOnError handles action =
liftIOAnnotated $ mapM_ IO.hClose handles
throwE e

-- | Wait for a process to exit by polling it. A thread blocked in 'IO.waitForProcess' cannot always
-- be cancelled: a cancel that arrives as the wait starts can be lost, and 'race' then waits until the
-- process exits.
pollForProcessExit :: (HasCallStack, MonadIO m) => IO.ProcessHandle -> m ExitCode
pollForProcessExit hProcess = liftIO $
IO.getProcessExitCode hProcess >>= \case
Just exitCode -> pure exitCode
Nothing -> threadDelay 100_000 >> pollForProcessExit hProcess

-- Sometimes even when we close the files manually, the operating system still holds the lock for some
-- reason. This is most prominent on MacOS. Therefore, as a last resort, instead of
-- failing the node startup procedure, we simply try to use a different file name for the logs, with
Expand Down
Loading