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

SOMETIMES readProcessStderr returns an empty string ignoring the output of the child #32

Open
igrep opened this issue Jun 17, 2020 · 7 comments

Comments

@igrep
Copy link

igrep commented Jun 17, 2020

UPDATE:

  • Modify the command to reproduce to make sure typed-process is installed.
  • Replace .\example.exe with ./example in repro.hs for easier testing on other OSs.
  • Make example.hs and repro.hs smaller.

Minimum code to reproduce

I'm not sure this is really the smallest code, but these reproduce the error relatively more often.

example.hs:

main :: IO ()
main = do
  _ <- getLine
  fail "Invalid input!!"

repro.hs:

{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import Control.Monad.IO.Class
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString as B
import System.Exit
import System.IO
import System.Process.Typed

main :: IO ()
main = do
  replicateM_ 300 $ do
    (CommandResult _ out) <- runCommand
    if (out == "")
      then fail "Reproduced!"
      else putStrLn "."

data CommandResult =
  CommandResult
    !ExitCode
    !B.ByteString
    deriving Show

runCommand :: IO CommandResult
runCommand = do
  let prc = setStdin (byteStringInput "\n")
        $ proc "./example" []
  (ecode, out) <- readProcessStderr prc
  return . CommandResult ecode $ BL.toStrict out

Steps to reproduce

stack --resolver=lts-16.1 exec ghc example.hs
stack --resolver=lts-16.1 runghc --package typed-process repro.hs

Expected result

stack --resolver=lts-16.1 runghc repro.hs should print only ., without any error.

Actual result

stack --resolver=lts-16.1 runghc repro.hs exits with an error after running the command several times.
For example:

> stack --resolver=lts-16.1 runghc repro.hs
.
.
.
.
.
repro.hs: user error (Reproduced!)

NOTE

Sometimes the error is not reproduced even after running 300 times.

My environment

Windows 10 Pro ver. 1809

@notogawa
Copy link

On MacOS 10.15.4, repro.hs (replace example.exe to ./example) generates expected result.

@igrep
Copy link
Author

igrep commented Jun 17, 2020

It wasn't reproduced also on my Debian 10 VM.
It actually seems a Windows specific bug.

@snoyberg
Copy link
Member

Can you try compiling with the multithreaded runtime instead? And does this occur with readProcess?

@igrep
Copy link
Author

igrep commented Jun 18, 2020

Unfortunately, -threaded didn't solve... 😞

> stack --resolver=lts-16.1 ghc --package typed-process -- -threaded repro.hs
[1 of 1] Compiling Main             ( repro.hs, repro.o )
Linking repro.exe ...
> .\repro.exe
.
.
.
.
.
.
.
.
.
.
.
.
.
.
repro.exe: user error (Reproduced!)

@igrep
Copy link
Author

igrep commented Jun 19, 2020

And found readProcess also causes the same problem.

repro-readProcess.hs:

{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString as B
import System.Exit
import System.IO
import System.Process.Typed

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  replicateM_ 300 $ do
    (CommandResult _ _out err) <- runCommand
    if err == ""
      then fail "Reproduced!"
      else putStrLn "."

data CommandResult =
  CommandResult
    !ExitCode
    !B.ByteString
    !B.ByteString
    deriving Show

runCommand :: IO CommandResult
runCommand = do
  let prc = setStdin (byteStringInput "bbbbb\nbbbbbbbbbddd\nccc\nkjkjk\ndafadfae\n\np\n")
        $ proc "./example" []
  (ecode, out, err) <- readProcess prc
  return $ CommandResult ecode (BL.toStrict out) (BL.toStrict err)
> stack --resolver=lts-16.1 ghc --package typed-process -- -threaded .\repro-readProcess.hs
[1 of 1] Compiling Main             ( repro-readProcess.hs, repro-readProcess.o )
Linking repro-readProcess.exe ...
> .\repro-readProcess.exe
.
.
.
.
.
.
.
.
.
.
.
repro-readProcess.exe: user error (Reproduced!)

@igrep
Copy link
Author

igrep commented Jun 19, 2020

One point more: Deleting all getLines from example.hs makes the problem stop happening (both in repro-readProcess.hs and repro.hs).

@snoyberg
Copy link
Member

I don't really have any thoughts on what may be causing this, sorry.

@igrep igrep changed the title SOMETIMES readProcessInterleaved returns an empty string ignoring the output of the child SOMETIMES readProcessStderr returns an empty string ignoring the output of the child Jun 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants