-
Couldn't load subscription status.
- Fork 45
Description
Describe the bug
The StandardInputWriter is not writing or flushing its content until it is deallocated.
To Reproduce
import Testing
import System
import Subprocess
class TestSubprocessTests {
@Test func example() async throws {
var retainedInputIO: StandardInputWriter?
_ = try await Subprocess.run(
.path("/bin/zsh"),
arguments: Arguments(["-c"] + ["cat"]),
environment: .inherit,
workingDirectory: nil)
{ execution, inputIO, outputIO, errorIO in
retainedInputIO = inputIO
Task {
let bytesWritten = try await inputIO.write("Hello, world!")
print("bytesWritten: \(bytesWritten)")
}
Task {
for try await line in outputIO.lines() {
print("received line: \(line)")
}
}
}
_ = retainedInputIO
}
}Expected behavior
With the line retainedInputIO = inputIO commented out, the test output is
Test example() started.
bytesWritten: 13
received line: Hello, world!
Test example() passed after 0.008 seconds.
and with this line uncommented I do not see anything received in stdout. The test starts (and never completes, because the program keeps running). Output:
Test example() started.
bytesWritten: 13
<test hangs>
For context, I'm working with a long lived process (MCP over stdio) and I need to be able to continuously read from and write to the subprocess. It also seems that the StandardInputWriter has no flush API that I could use to remediate this issue. Adding new lines to the written content doesn't help with flushing