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

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

wants to merge 1 commit into from

Conversation

mitchellwrosen
Copy link
Collaborator

Hi Jonathan,

I looked into debugging this start error that I was seeing (note the empty stdout/stderr):

InitDbFailed {startErrorStdOut = "", startErrorStdErr = "", startErrorExitCode = ExitFailure 1}

And with this patch, it instead looks like

InitDbFailed {startErrorStdOut = "", startErrorStdErr = "initdb: unrecognized option `--no-sync'Try \"initdb --help\" for more information.", startErrorExitCode = ExitFailure 1}

I'll annotate the PR with more info about what's going on

Copy link

@hint-man hint-man bot left a comment

Choose a reason for hiding this comment

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

There is no place for me here... I will choose the truth I like.

@@ -132,7 +132,7 @@ teeHandle orig f =
modifyIORef outputRef (<>theLine)
hPutStrLn orig theLine

res <- withAsync readerLoop $ \_ -> f writeEnd
res <- withAsync readerLoop $ f writeEnd
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The callback is now provided a handle to the background thread that's reading readEnd.

Previously, after initdb finished, the callback was closed immediately, which canceled this thread before it could read the pipe until EOF.

{ 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.

@jfischoff
Copy link
Owner

I'm happy to merge this PR as is but I think if would be best to add test case that fails without this PR and passes with it.

@mitchellwrosen
Copy link
Collaborator Author

Will do

@mitchellwrosen
Copy link
Collaborator Author

Hm. I attempted to add a failing test and follow it up with the commit that fixes it, but the commit that was supposed to fail passed CI. It's racy - the background threads might exhaust the handles before they're killed, which makes the test pass.

Maybe I could just reword the commit to not claim the test is "failing", follow up with the original commit, and call it good? :)

@jfischoff
Copy link
Owner

Hmm. My 500 foot view tells me there are still some mysteries here to solve. I'll try to take a look just to make sure I understand the problem and the fix. Thanks for adding the test ... I'll see if I can help make it reproducible.

@@ -128,6 +128,7 @@ teeHandle orig f =
outputRef <- newIORef []

let readerLoop = forever $ do
threadDelay 1000000
Copy link
Owner

Choose a reason for hiding this comment

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

👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah; just added this temporarily to show where it's racy

@mitchellwrosen
Copy link
Collaborator Author

Hm. Live-lock 👀

@jfischoff
Copy link
Owner

Hmm it looks like your fix is not passing your test

@mitchellwrosen
Copy link
Collaborator Author

I'm not sure what the issue was, but I reworked the code a little bit and got the tests passing. Not very reassuring but... I think the logic checks out. Want me to restate what the issue was, and why this patch fixes it?

@jfischoff
Copy link
Owner

Just saw your new changes. Going to review them soon.

Want me to restate what the issue was, and why this patch fixes it?

Sure

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

Successfully merging this pull request may close these issues.

2 participants