turtle icon indicating copy to clipboard operation
turtle copied to clipboard

commandStreaming hangs if output is large

Open AndrazP opened this issue 2 years ago • 2 comments

Describe the bug Reopening https://github.com/lordcodes/turtle/issues/121 If the output of a shell command is really large then commandStreaming will hang.

Checklist

To Reproduce run a command like git cat-file -p origin/main:. In our case the file was a 4000 line long OpenAPI spec.

Expected behavior Dumps file output

Additional context

Class Process

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

Fail to clear the buffer of input stream (which pipes to the output stream of subprocess) from Process may lead to a subprocess blocking.

You can read more about how to fix it in this Stackoverflow thread.

AndrazP avatar Sep 12 '23 17:09 AndrazP

I suggest returning the result as a Kotlin Sequence.

  private fun commandSequence(
    command: List<String>,
    workingDirectory: File? = null
  ): Sequence<String> {

    val processBuilder = ProcessBuilder(command).directory(workingDirectory).redirectErrorStream(true)
    val process = processBuilder.start()

    val reader = BufferedReader(InputStreamReader(process.inputStream))

    return sequence {
      var line: String?
      while (reader.readLine().also { line = it } != null) {
        yield(line!!)
      }

      val exitCode = process.waitFor()
      if (exitCode != 0) {
        throw RuntimeException("Command execution failed with exit code: $exitCode")
      }
    }
  }

AndrazP avatar Sep 14 '23 15:09 AndrazP

Thank you for the extra information. If anyone wanted to contribute a PR, I'm happy to review it.

lordcodes avatar Sep 14 '23 22:09 lordcodes

@AndrazP I had some time and decided to look into this.

lordcodes avatar May 06 '24 14:05 lordcodes