-
Notifications
You must be signed in to change notification settings - Fork 70
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
Prevent stray output from reaching stdout #618
Conversation
On second thought, not switching System.out back may cause stray prints become data races as the main thread reads from But if that is the reason for calling Or if the writes synchronize over the BAOS as I suggested in #619, the data race can be prevented by synchronized(outputStream) {
outputObj.text = outputStream.toString()
.replace("</errStream><errStream>".toRegex(), "")
.replace("</outStream><outStream>".toRegex(), "")
} |
Hi, is it possible to add a test (just a snippet to run)? |
17af9a0
to
d2a0392
Compare
) | ||
Assertions.assertEquals("<outStream>I'm sleeping 0 ...\nI'm sleeping 1 ...\nI'm sleeping 2 ...\n</outStream>", result.text) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you go before the fix with git checkout 5e25435
, this test will intermittently fail.
// coroutines can produced incorrect output. see example in `base coroutines test 7` | ||
if (standardOutput.startsWith("{")) outputMapper.readValue(standardOutput, ExecutionResult::class.java) | ||
else { | ||
val result = outputMapper.readValue("{" + standardOutput.substringAfter("{"), ExecutionResult::class.java) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we won't need this workaround, but I wouldn't object to keeping it.
} | ||
catch (e: Exception) { | ||
System.setOut(standardOutput) | ||
print("[\"") | ||
standardOutput.print("[\"") | ||
e.printStackTrace() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that it's writing to PrintStream(errorStream)
. Then the BAOS is discarded. Should it be sent to stderr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, hard to say. The code in the catch
block seems nonfunctional, regardless of the changes. I tried to reproduce a case when the execution flow reached this part of the code and got a serialization error.
Is the modified test sufficient? |
Following up on this. I wasn't very clear in my previous comment. I have changed the test which now guards against the bug. I don't think a new additional test is needed. The test that I modified, |
This bug has confused another person. |
I found another SO question caused by this bug. |
@phiSgr, thank you for bringing this up! |
No objections from my side. |
@nikpachoo, thanks for merging this. It seems to me that it's now deployed to https://play.kotlinlang.org only in version 2.0.0-RC2. Will it be back-ported to older versions as well? |
@phiSgr, |
prevents prints that happen after (user's)
main
return
s from polluting stdout.Context: https://stackoverflow.com/questions/75714151/kotlin-coroutines-order-of-printing-logs/75760394#75760394