Skip to content

Remove the default output buffer limit #45

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions Sources/Subprocess/IO/Output.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public protocol OutputProtocol: Sendable, ~Copyable {
#endif
extension OutputProtocol {
/// The max amount of data to collect for this output.
public var maxSize: Int { 128 * 1024 }
public var maxSize: Int { .max }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iCharlesHu Why are we fixing a hang with increasing the buffer size to infinity? That's not quite the right fix I think.

if you're not streaming and you blow through the max size you should get an error, not a hang. That's also what AsyncProcess does.

in fact, you will now see a hang (followed by an OOM kill) if you run for example cat /dev/zero.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your point, but struggling a bit to find the right answer here. If we put a limit in, and during testing someone only uses file sizes < 128k (for example), then a customer of the app is the one who hits the error. If we are unlimited, then it'll just use infinite memory, which is also a problem.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safe and unexpected is better than unsafe an unexpected IMHO. So a limit is IMHO a must.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you get an error telling you "too much data to collect", it's immediately actionable. If your process just grinds to a halt or gets OOM killed, you need to do much more work to even figure out what the problem is.

}

/// A concrete `Output` type for subprocesses that indicates that
Expand Down Expand Up @@ -240,10 +240,12 @@ extension OutputProtocol where Self == FileDescriptorOutput {
@available(SubprocessSpan, *)
#endif
extension OutputProtocol where Self == StringOutput<UTF8> {
/// Create a `Subprocess` output that collects output as
/// UTF8 String with 128kb limit.
/// Create a `Subprocess` output that collects output as UTF8 String
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the documentation comment here should say that the buffer is unlimited, so the amount of memory required is proportional to the size of the output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good

/// with an unlimited buffer size. The memory requirement for collecting
/// output is directly proportional to the size of the output
/// emitted by the child process.
public static var string: Self {
.init(limit: 128 * 1024, encoding: UTF8.self)
.init(limit: .max, encoding: UTF8.self)
}
}

Expand All @@ -265,9 +267,11 @@ extension OutputProtocol {
@available(SubprocessSpan, *)
#endif
extension OutputProtocol where Self == BytesOutput {
/// Create a `Subprocess` output that collects output as
/// `Buffer` with 128kb limit.
public static var bytes: Self { .init(limit: 128 * 1024) }
/// Create a `Subprocess` output that collects output as a `Buffer`
/// with an unlimited buffer size. The memory requirement for collecting
/// output is directly proportional to the size of the output
/// emitted by the child process.
public static var bytes: Self { .init(limit: .max) }

/// Create a `Subprocess` output that collects output as
/// `Buffer` up to limit it bytes.
Expand Down
19 changes: 19 additions & 0 deletions Tests/SubprocessTests/SubprocessTests+Unix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,25 @@ extension SubprocessUnixTests {
}
#expect(result == .unhandledException(SIGKILL))
}

@Test func testUnlimitedBufferByDefault() async throws {
guard #available(SubprocessSpan , *) else {
return
}

// Make sure we can read long text from standard input
let expected: Data = try Data(
contentsOf: URL(filePath: theMysteriousIsland.string)
)
// Launch cat with default output `.string`
let cat = try await Subprocess.run(
.path("/bin/cat"),
arguments: ["\(theMysteriousIsland.string)"]
)
#expect(cat.terminationStatus.isSuccess)
// Make sure we read all bytes
#expect(cat.standardOutput!.data(using: .utf8) == expected)
}
}

// MARK: - Utils
Expand Down