Skip to content

Commit

Permalink
Merge pull request #614 from jflan-dd/jflan/pipe-overfill
Browse files Browse the repository at this point in the history
Drain stdout pipe while command is running
  • Loading branch information
f-meloni authored Oct 14, 2024
2 parents 8f0bf9d + e4e7167 commit b22f43c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

## Master

- Drain stdout while shell commands are running to prevent execution from locking up if there's too much output [@jflan-dd][] - [#614](https://github.com/danger/swift/pull/614)

## 3.20.0
- Remove deprecated `lint` function with `lintAllFiles` flag [@417-72KI][] - [#622](https://github.com/danger/swift/pull/622)
- Updated Swift 6 build process: Danger files moved to .build/debug/Modules, and SwiftFormat module map conflict resolved by adjusting the Swift import search path. [@abhi-m-simformsolutons][] -[#626](https://github.com/danger/swift/pull/626)
Expand Down
11 changes: 8 additions & 3 deletions Sources/DangerShellExecutor/ShellExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ public struct ShellExecutor: ShellExecuting {
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
task.waitUntilExit()

let data = pipe.fileHandleForReading.readDataToEndOfFile()

task.waitUntilExit()

return String(data: data, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines)
}

Expand All @@ -83,19 +85,22 @@ public struct ShellExecutor: ShellExecuting {
let stderr = Pipe()
task.standardError = stderr
task.launch()
task.waitUntilExit()

// Pull out the STDOUT as a string because we'll need that regardless
let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile()
let stdoutString = String(data: stdoutData, encoding: .utf8)!

// Read from STDERR to ensure the `Pipe` does not fill up
let stderrData = stderr.fileHandleForReading.readDataToEndOfFile()

task.waitUntilExit()

// 0 is no problems in unix land
if task.terminationStatus == 0 {
return stdoutString.trimmingCharacters(in: .whitespacesAndNewlines)
}

// OK, so it failed, raise a new error with all the useful metadata
let stderrData = stderr.fileHandleForReading.readDataToEndOfFile()
let stderrString = String(data: stderrData, encoding: .utf8)!

throw SpawnError.commandFailed(command: command,
Expand Down

0 comments on commit b22f43c

Please sign in to comment.