Changesets · Branches · Tags · Compare
| Changeset | 554dc5ae3b37dcaad620fc51206de49eef9358358a1541a9e1c52469e66cb9f3 |
|---|---|
| Author | Jakub Blach <jakubblach@gmail.com> · 2025-07-07 15:47 +02:00 |
| Committer | Jakub Blach <jakubblach@gmail.com> · 2025-07-08 14:27 +02:00 |
| Parents | c7054bc87e23 |
2 files changed
modified extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/continue/process/ContinueProcessHandler.kt +17 −14
| @@ -2,41 +2,43 @@ | ||
| 2 | 2 | |
| 3 | 3 | import com.github.continuedev.continueintellijextension.error.ContinueErrorService |
| 4 | 4 | import com.intellij.openapi.components.service |
| 5 | -import kotlinx.coroutines.CoroutineScope | |
| 6 | -import kotlinx.coroutines.Dispatchers | |
| 5 | +import kotlinx.coroutines.* | |
| 7 | 6 | import kotlinx.coroutines.channels.Channel |
| 8 | -import kotlinx.coroutines.delay | |
| 9 | -import kotlinx.coroutines.launch | |
| 10 | 7 | import java.io.BufferedReader |
| 8 | +import java.io.IOException | |
| 11 | 9 | import java.io.InputStreamReader |
| 12 | 10 | import java.io.OutputStreamWriter |
| 13 | 11 | |
| 14 | 12 | class ContinueProcessHandler( |
| 15 | - coroutineScope: CoroutineScope, | |
| 13 | + parentScope: CoroutineScope, | |
| 16 | 14 | private val process: ContinueProcess, |
| 17 | 15 | handleMessage: (String) -> (Unit) |
| 18 | 16 | ) { |
| 19 | - private val pendingWrites = Channel<String>(Channel.Factory.UNLIMITED) | |
| 17 | + private val innerJob = Job() | |
| 18 | + private val scope = CoroutineScope(parentScope.coroutineContext + innerJob) | |
| 19 | + private val pendingWrites = Channel<String>(Channel.UNLIMITED) | |
| 20 | 20 | private val writer = OutputStreamWriter(process.output) |
| 21 | 21 | private val reader = BufferedReader(InputStreamReader(process.input)) |
| 22 | 22 | |
| 23 | 23 | init { |
| 24 | - coroutineScope.launch(Dispatchers.IO) { | |
| 25 | - while (true) { | |
| 26 | - if (reader.ready()) { | |
| 24 | + scope.launch(Dispatchers.IO) { | |
| 25 | + try { | |
| 26 | + while (isActive) { | |
| 27 | 27 | val line = reader.readLine() |
| 28 | - if (line.isNotEmpty()) { | |
| 28 | + if (line != null && line.isNotEmpty()) { | |
| 29 | 29 | try { |
| 30 | 30 | handleMessage(line) |
| 31 | 31 | } catch (e: Exception) { |
| 32 | 32 | service<ContinueErrorService>().report(e, "Error handling message: $line") |
| 33 | 33 | } |
| 34 | - } | |
| 35 | - } else | |
| 36 | - delay(100) | |
| 34 | + } else | |
| 35 | + delay(100) | |
| 36 | + } | |
| 37 | + } catch (e: IOException) { | |
| 38 | + service<ContinueErrorService>().report(e) | |
| 37 | 39 | } |
| 38 | 40 | } |
| 39 | - coroutineScope.launch(Dispatchers.IO) { | |
| 41 | + scope.launch(Dispatchers.IO) { | |
| 40 | 42 | for (message in pendingWrites) { |
| 41 | 43 | writer.write(message) |
| 42 | 44 | writer.write("\r\n") |
| @@ -49,6 +51,7 @@ | ||
| 49 | 51 | pendingWrites.trySend(message) |
| 50 | 52 | |
| 51 | 53 | fun close() { |
| 54 | + innerJob.cancel() | |
| 52 | 55 | reader.close() |
| 53 | 56 | writer.close() |
| 54 | 57 | process.close() |
added extensions/intellij/src/test/kotlin/com/github/continuedev/continueintellijextension/continue/process/ContinueProcessHandlerTest.kt +49 −0
| @@ -0,0 +1,49 @@ | ||
| 1 | +package com.github.continuedev.continueintellijextension.`continue`.process | |
| 2 | + | |
| 3 | +import kotlinx.coroutines.Dispatchers | |
| 4 | +import kotlinx.coroutines.joinAll | |
| 5 | +import kotlinx.coroutines.launch | |
| 6 | +import kotlinx.coroutines.test.runTest | |
| 7 | +import org.junit.jupiter.api.Assertions.assertTrue | |
| 8 | +import org.junit.jupiter.api.RepeatedTest | |
| 9 | +import java.io.InputStream | |
| 10 | +import java.io.OutputStream | |
| 11 | + | |
| 12 | +class ContinueProcessHandlerTest { | |
| 13 | + | |
| 14 | + @RepeatedTest(5) | |
| 15 | + fun `test concurrent writes with queue`() = runTest { | |
| 16 | + val process = StringContinueProcess() | |
| 17 | + val handler = ContinueProcessHandler(this, process) {} | |
| 18 | + | |
| 19 | + (0..100).map { i -> launch(Dispatchers.Default) { handler.write("message number $i") } } | |
| 20 | + .joinAll() | |
| 21 | + | |
| 22 | + handler.close() | |
| 23 | + | |
| 24 | + // assert all message lines are present (order not important) | |
| 25 | + val lines = process.string.split("\r\n").filter { it.isNotBlank() } | |
| 26 | + for (i in 0..100) | |
| 27 | + assertTrue(lines.contains("message number $i")) | |
| 28 | + | |
| 29 | + // note: this test will fail if the handler writes messages directly to the output stream | |
| 30 | + // this problem can be easily reproduced by removing the channel<string> from the handler code | |
| 31 | + } | |
| 32 | + | |
| 33 | + class StringContinueProcess : ContinueProcess { | |
| 34 | + val string = StringBuilder() | |
| 35 | + override val input: InputStream = InputStream.nullInputStream() | |
| 36 | + override val output: OutputStream = object : OutputStream() { | |
| 37 | + override fun write(b: Int) { | |
| 38 | + string.append(b.toChar()) | |
| 39 | + } | |
| 40 | + | |
| 41 | + override fun write(b: ByteArray, off: Int, len: Int) { | |
| 42 | + string.append(String(b, off, len)) | |
| 43 | + } | |
| 44 | + } | |
| 45 | + | |
| 46 | + override fun close() { | |
| 47 | + } | |
| 48 | + } | |
| 49 | +} | |
| No newline at the end of the file |