Skip to content

Processes with small and infrequent output doesn't get emitted in a timely manner when using sequence to capture the output #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rdingman opened this issue May 8, 2025 · 1 comment · May be fixed by #40
Labels
bug Something isn't working

Comments

@rdingman
Copy link

rdingman commented May 8, 2025

Describe the bug
I have a process I want to monitor that is long running. It has small and infrequent output that I need to get in as soon as it is available. I'm running this process with swift-subprocess and monitoring its output by using SequenceOutput. In the custom execution closure, I only get the output either when the process exits or when the output size reaches readBufferSize defined in AsyncBufferSequence.

To Reproduce

Here is some sample code that demonstrates the problem:

let script = """
echo "[NOTICE] Important log line"
sleep 3600
"""

let catResult = try await Subprocess.run(
    .path("/bin/bash"),
    arguments: ["-c", script],
    output: .sequence,
    error: .discarded,
    body: { (execution, _) in
        for try await chunk in execution.standardOutput {
            let string = chunk.withUnsafeBytes { String(decoding: $0, as: UTF8.self) }

            if string.hasPrefix("[NOTICE]") {
                // Do Something interesting
                print(string)
            }
        }

        return ""
    }
)

Expected behavior

There should be a mechanism for getting the output as soon as it is available.

Environment (please complete the following information):

  • macOS 15
  • swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)

Additional context

I'll follow up with a PR that has the sketch for a proposed solution.

@rdingman rdingman added the bug Something isn't working label May 8, 2025
rdingman added a commit to rdingman/swift-subprocess that referenced this issue May 8, 2025
when using SequenceOutput to emit standard output and standard error
as soon as it arrives.

Also, fixed a bug where DispatchIO.read could end up resuming a
Swift continuation multiple times

Resolves swiftlang#39
rdingman added a commit to rdingman/swift-subprocess that referenced this issue May 8, 2025
when using SequenceOutput to emit standard output and standard error
as soon as it arrives.

Resolves swiftlang#39
@marlimox
Copy link
Contributor

+1

I've also run into this issue with a project I'm working on that uses Subprocess. Excited to see you're working on a fix 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants