Skip to content

Commit 8229880

Browse files
fix clean up memory in log task executor
1 parent 0515b3f commit 8229880

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Source/Swiftline/CommandExecutor.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,28 @@ class LogTaskExecutor: TaskExecutor {
9696

9797
func execute(_ commandParts: [String]) -> ExecutorReturnValue {
9898
let argv: [UnsafeMutablePointer<CChar>?] = commandParts.map{ $0.withCString(strdup) }
99-
defer { for case let arg? in argv { free(arg) } }
100-
10199
var pid: pid_t = 0
102100
var childFDActions: posix_spawn_file_actions_t? = nil
103101
let outputPipe: Int32 = 69
104102
let outerrPipe: Int32 = 70
105103

104+
defer {
105+
for case let arg? in argv { free(arg) }
106+
posix_spawn_file_actions_addclose(&childFDActions, outputPipe)
107+
posix_spawn_file_actions_addclose(&childFDActions, outerrPipe)
108+
posix_spawn_file_actions_destroy(&childFDActions)
109+
}
110+
106111
posix_spawn_file_actions_init(&childFDActions)
107112
posix_spawn_file_actions_addopen(&childFDActions, outputPipe, stdoutLogPath, O_CREAT | O_TRUNC | O_WRONLY, ~0)
108113
posix_spawn_file_actions_addopen(&childFDActions, outerrPipe, stderrLogPath, O_CREAT | O_TRUNC | O_WRONLY, ~0)
109114
posix_spawn_file_actions_adddup2(&childFDActions, outputPipe, 1)
110115
posix_spawn_file_actions_adddup2(&childFDActions, outerrPipe, 2)
111116

112117
var result = posix_spawn(&pid, argv[0], &childFDActions, nil, argv + [nil], nil)
113-
guard result == 0 else { return (Int(1), "", "") }
114-
118+
guard result == 0 else { return (Int(result), "", "") }
115119
waitpid(pid, &result, 0)
116-
posix_spawn_file_actions_addclose(&childFDActions, outputPipe)
117-
posix_spawn_file_actions_addclose(&childFDActions, outerrPipe)
118-
posix_spawn_file_actions_destroy(&childFDActions)
119-
120+
120121
let (stdout, stderr) = read(outputPath: stdoutLogPath, outerrPath: stderrLogPath)
121122
removeFiles(stdoutLogPath, stderrLogPath)
122123
write(atPath: logPath, content: "\(stdout)\n\(stderr)")

0 commit comments

Comments
 (0)