Skip to content
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

Wait for tee'd process stdout/stderr to hit EOF #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
35 changes: 20 additions & 15 deletions src/Database/Postgres/Temp/Internal/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ See 'startPlan' for more details.
module Database.Postgres.Temp.Internal.Core where

import Control.Concurrent
import Control.Concurrent.Async (race_, withAsync)
import Control.Concurrent.Async (Async, race_, wait, withAsync)
import Control.Exception
import Control.Monad
import qualified Data.ByteString.Char8 as BSC
import Data.Foldable (for_)
import Data.IORef
import Data.Typeable
import qualified Database.PostgreSQL.Simple as PG
import qualified Database.PostgreSQL.Simple.Options as Client
Expand Down Expand Up @@ -122,18 +121,19 @@ waitForDB logger options = do
Right () -> return ()

-- Only useful if we believe the output is finite
teeHandle :: Handle -> (Handle -> IO a) -> IO (a, String)
teeHandle :: Handle -> (Handle -> Async String -> IO a) -> IO a
teeHandle orig f =
bracket createPipe (\(x, y) -> hClose x >> hClose y) $ \(readEnd, writeEnd) -> do
outputRef <- newIORef []
let readerLoop acc = do
eof <- hIsEOF readEnd
if eof then
pure acc
else do
theLine <- hGetLine readEnd
hPutStrLn orig theLine
readerLoop (acc <> theLine)

let readerLoop = forever $ do
theLine <- hGetLine readEnd
modifyIORef outputRef (<>theLine)
hPutStrLn orig theLine

res <- withAsync readerLoop $ \_ -> f writeEnd
(res,) <$> readIORef outputRef
withAsync (readerLoop "") $ f writeEnd

-- | 'CompleteProcessConfig' contains the configuration necessary for starting a
-- process. It is essentially a stripped down 'System.Process.CreateProcess'.
Expand Down Expand Up @@ -212,13 +212,18 @@ executeProcessAndTee
-> CompleteProcessConfig
-- ^ Process config
-> IO (ExitCode, String, String)
executeProcessAndTee name config = fmap (\((x, y), z) -> (x, z, y)) $
teeHandle (completeProcessConfigStdOut config) $ \newOut ->
teeHandle (completeProcessConfigStdErr config) $ \newErr ->
executeProcess name $ config
executeProcessAndTee name config =
teeHandle (completeProcessConfigStdOut config) $ \newOut aOut ->
teeHandle (completeProcessConfigStdErr config) $ \newErr aErr -> do
result <- executeProcess name $ config
{ completeProcessConfigStdErr = newErr
, completeProcessConfigStdOut = newOut
}
for_ [newOut, newErr] hClose -- executeProcess doesn't close these
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

executeProcess doesn't close newErr or newOut because it uses createProcess_ internally, so we explicitly do that now. This causes the background threads to die eventually when they try to hGetLine on a handle that's hit EOF.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I wasn't totally sure about: we close the write ends here (so the read threads die), but we also close the write ends in the cleanup function of the bracketed createPipe. So, the handles are closed twice. Things seem to work, though; I guess I'd have expected that to be an IO exception.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, scratch that. From the docs:

Performing hClose on a handle that has already been closed has no effect; doing so is not an error.

stdoutString <- wait aOut
stderrString <- wait aErr
pure (result, stdoutString, stderrString)

-------------------------------------------------------------------------------
-- PostgresProcess Life cycle management
-------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,10 @@ spec = do
PG.query_ conn "SELECT id FROM foo ORDER BY id ASC"
`shouldReturn` [PG.Only (1 :: Int)]

it "executeProcessAndTee doesn't lose stderr" do
let config = CompleteProcessConfig [] ["--bogus-argument"] devNull devNull devNull
let expected = "could not find a \"initdb\" to executeinitdb: unrecognized option '--bogus-argument'Try \"initdb --help\" for more information."
executeProcessAndTee "initdb" config `shouldReturn` (ExitFailure 1, "", expected)

main :: IO ()
main = hspec spec