Skip to content

Commit

Permalink
Wait for server process in client_server_test (#12551)
Browse files Browse the repository at this point in the history
withCreateProcess kills the server process but it doesn’t wait for it
to finish. The `typed-process` version does things properly here which
is also why we switched to it in other places.

changelog_begin
changelog_end
  • Loading branch information
cocreature authored Jan 24, 2022
1 parent f1cd4b1 commit 5bfe4ed
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ da_haskell_binary(
hackage_deps = [
"base",
"extra",
"typed-process",
"process",
"async",
"text",
Expand Down
14 changes: 9 additions & 5 deletions bazel_tools/client_server/runner_with_port_check/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ module Main(main) where
import System.Environment
import Control.Exception.Safe
import Control.Monad.Loops (untilJust)
import System.Process
import System.Process.Typed
import Data.List.Split (splitOn)
import Control.Monad (forM_)
import Control.Monad (forM_, when)
import Network.Socket
import Control.Concurrent
import System.Info.Extra
import System.Process (terminateProcess)

main :: IO ()
main = do
[clientExe, clientArgs, serverExe, serverArgs, runnerArgs] <- getArgs
let splitArgs = filter (/= "") . splitOn " "
let serverProc = proc serverExe (splitArgs serverArgs)
let ports :: [Int] = read <$> splitArgs runnerArgs
withCreateProcess serverProc $ \_stdin _stdout _stderr _ph -> do
withProcessTerm serverProc $ \ph -> do
forM_ ports $ \port -> waitForConnectionOnPort (threadDelay 500000) port
callProcess clientExe (splitArgs clientArgs)
runProcess_ (proc clientExe (splitArgs clientArgs))
-- See the comment on DA.Daml.Helper.Util.withProcessWait_'
when isWindows (terminateProcess $ unsafeProcessHandle ph)


waitForConnectionOnPort :: IO () -> Int -> IO ()
Expand All @@ -36,4 +40,4 @@ waitForConnectionOnPort sleep port = do
checkConnection addr = bracket
(socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr))
close
(\s -> connect s (addrAddress addr))
(\s -> connect s (addrAddress addr))
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ da_haskell_binary(
"directory",
"extra",
"filepath",
"typed-process",
"process",
],
visibility = ["//visibility:public"],
Expand Down
14 changes: 9 additions & 5 deletions bazel_tools/client_server/runner_with_port_file/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@

module Main (main) where

import Control.Monad (unless)
import Control.Monad (unless, when)
import Data.List.Extra (replace, splitOn, stripInfix)
import Data.Maybe (isJust)
import System.Environment (getArgs)
import System.FilePath ((</>))
import System.Process (callProcess, proc, withCreateProcess)
import System.Process.Typed (proc, runProcess_, withProcessTerm, unsafeProcessHandle)
import System.Exit (exitFailure)
import System.IO (hPutStrLn, stderr)
import System.IO.Extra (withTempDir)
import System.Info.Extra
import System.Process (terminateProcess)

import DA.PortFile

Expand All @@ -28,7 +30,9 @@ main = do
let portFile = tempDir </> "portfile"
let interpolatedServerArgs = map (replace "%PORT_FILE%" portFile) splitServerArgs
let serverProc = proc serverExe interpolatedServerArgs
withCreateProcess serverProc $ \_stdin _stdout _stderr ph -> do
port <- readPortFile ph maxRetries portFile
withProcessTerm serverProc $ \ph -> do
port <- readPortFile (unsafeProcessHandle ph) maxRetries portFile
let interpolatedClientArgs = map (replace "%PORT%" (show port)) splitClientArgs
callProcess clientExe interpolatedClientArgs
runProcess_ (proc clientExe interpolatedClientArgs)
-- See the comment on DA.Daml.Helper.Util.withProcessWait_'
when isWindows (terminateProcess $ unsafeProcessHandle ph)

0 comments on commit 5bfe4ed

Please sign in to comment.