1/2 Make OutputWrapper thread-safe

OutputWrapper is accessed from different Gradle thread pool threads.

You can reproduce it by running
`ProjectTemplateNewWizardProjectImportTestGenerated.GradleKts.testComposeMultiplatformApplication`
and checking from which threads the same instance of OutputWrapper is
accessed.

This commit is an attempt to fight against the
`testComposeMultiplatformApplication` test flakiness.

The test flaky failure stacktrace:

  java.lang.StringIndexOutOfBoundsException: begin 0, end 182, length 144
    at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4604)
    at java.base/java.lang.String.substring(String.java:2707)
    at com.intellij.execution.process.AnsiStreamingLexer.getElementText(AnsiStreamingLexer.java:50)
    at com.intellij.execution.process.AnsiStreamingLexer.getElementTextSmart(AnsiStreamingLexer.java:62)
    at com.intellij.execution.process.AnsiEscapeDecoder.escapeText(AnsiEscapeDecoder.java:54)
    at com.intellij.openapi.externalSystem.service.execution.ExternalSystemProcessHandler.notifyTextAvailable(ExternalSystemProcessHandler.java:65)
    at com.intellij.openapi.externalSystem.util.ExternalSystemUtil$2$3.onTaskOutput(ExternalSystemUtil.java:442)
    at com.intellij.openapi.externalSystem.service.remote.ExternalSystemProgressNotificationManagerImpl$TaskListenerWrapper.onTaskOutput(ExternalSystemProgressNotificationManagerImpl.kt:111)
    at jdk.internal.reflect.GeneratedMethodAccessor959.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at com.intellij.util.EventDispatcher.dispatchVoidMethod(EventDispatcher.java:120)
    at com.intellij.util.EventDispatcher.lambda$createMulticaster$1(EventDispatcher.java:85)
    at jdk.proxy2/jdk.proxy2.$Proxy235.onTaskOutput(Unknown Source)
    at com.intellij.openapi.externalSystem.service.remote.ExternalSystemProgressNotificationManagerImpl$onTaskOutput$1.invoke(ExternalSystemProgressNotificationManagerImpl.kt:47)
    at com.intellij.openapi.externalSystem.service.remote.ExternalSystemProgressNotificationManagerImpl$onTaskOutput$1.invoke(ExternalSystemProgressNotificationManagerImpl.kt:47)
    at com.intellij.openapi.externalSystem.service.remote.ExternalSystemProgressNotificationManagerImpl.forEachListener(ExternalSystemProgressNotificationManagerImpl.kt:90)
    at com.intellij.openapi.externalSystem.service.remote.ExternalSystemProgressNotificationManagerImpl.onTaskOutput(ExternalSystemProgressNotificationManagerImpl.kt:47)
    at com.intellij.openapi.externalSystem.service.AbstractExternalSystemFacadeImpl$SwallowingNotificationListener.onTaskOutput(AbstractExternalSystemFacadeImpl.java:240)
    at com.intellij.openapi.externalSystem.util.OutputWrapper.doFlush(OutputWrapper.java:50)
    at com.intellij.openapi.externalSystem.util.OutputWrapper.flush(OutputWrapper.java:43)
    at java.base/sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:320)
    at java.base/sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:160)
    at java.base/java.io.OutputStreamWriter.flush(OutputStreamWriter.java:248)

IJ-MR-26323

GitOrigin-RevId: e014a12c364de0afdbb5bfd5713fe5677a21fa98
This commit is contained in:
Nikita Bobko
2022-06-24 15:36:08 +02:00
committed by intellij-monorepo-bot
parent ceddd8f458
commit bb9a58b3ae

View File

@@ -23,7 +23,7 @@ public class OutputWrapper extends OutputStream {
}
@Override
public void write(int b) {
public synchronized void write(int b) {
if (myBuffer == null) {
myBuffer = new StringBuilder();
}
@@ -31,7 +31,7 @@ public class OutputWrapper extends OutputStream {
}
@Override
public void write(byte[] b, int off, int len) {
public synchronized void write(byte[] b, int off, int len) {
if (myBuffer == null) {
myBuffer = new StringBuilder();
}
@@ -39,7 +39,7 @@ public class OutputWrapper extends OutputStream {
}
@Override
public void flush() {
public synchronized void flush() {
doFlush();
}