kotlin-compiler-server icon indicating copy to clipboard operation
kotlin-compiler-server copied to clipboard

Prevent stray output from reaching stdout

Open phiSgr opened this issue 1 year ago • 5 comments

prevents prints that happen after (user's) main returns from polluting stdout.

Context: https://stackoverflow.com/questions/75714151/kotlin-coroutines-order-of-printing-logs/75760394#75760394

phiSgr avatar Mar 18 '23 04:03 phiSgr

On second thought, not switching System.out back may cause stray prints become data races as the main thread reads from ByteArrayOutputStream and the thread the prints writes to it.

But if that is the reason for calling setOut, System.err should also be switched back.


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(), "")
}

phiSgr avatar Mar 21 '23 01:03 phiSgr

Hi, is it possible to add a test (just a snippet to run)?

AlexanderPrendota avatar Mar 21 '23 09:03 AlexanderPrendota

possible to add a test

Is the modified test sufficient? https://github.com/JetBrains/kotlin-compiler-server/pull/618#discussion_r1148465908

phiSgr avatar Mar 28 '23 19:03 phiSgr

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, base coroutines test 7 used to only test for substring match. Now it is testing for full equality.

phiSgr avatar Jul 17 '23 05:07 phiSgr

This bug has confused another person.

https://stackoverflow.com/questions/77619172/why-does-the-kotlin-coroutine-output-a-b-even-with-a-delay

phiSgr avatar Dec 10 '23 05:12 phiSgr

I found another SO question caused by this bug. Imagine how many people are confused by this but didn't ask a question.

https://stackoverflow.com/questions/71918773/why-does-coroutine-in-background-execute-before-the-main-thread-in-this-kotlin-c

phiSgr avatar Apr 19 '24 17:04 phiSgr

@phiSgr, thank you for bringing this up! We are reviewing the PR, and I hope we can merge it this week.

nikpachoo avatar Apr 22 '24 08:04 nikpachoo

No objections from my side. @phiSgr, thank you for the contribution!

nikpachoo avatar Apr 28 '24 12:04 nikpachoo

@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 avatar May 01 '24 16:05 phiSgr

@phiSgr, 1.9.24 version released today contains the fix ✅ There are no plans to back-port it to all the previous versions; their usage is relatively low. Thank you for your participation ❤️

nikpachoo avatar May 07 '24 13:05 nikpachoo