commandStreaming hangs if output is large
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
- [x] I've read the guide for contributing.
- [x] I've checked there are no other open pull requests for the issue.
- [x] I've checked there are no other open issues for the same issue.
To Reproduce
run a command like git cat-file -p origin/main:
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.
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")
}
}
}
Thank you for the extra information. If anyone wanted to contribute a PR, I'm happy to review it.
@AndrazP I had some time and decided to look into this.