Skip to content
Merged
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
3 changes: 3 additions & 0 deletions Sources/Containerization/LinuxContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,9 @@ extension LinuxContainer {
)
}

// Lets free up the init procs resources, as this includes the open agent conn.
try? await startedState.process.delete()

try await startedState.vm.stop()
try state.stopped()
}
Expand Down
26 changes: 23 additions & 3 deletions Sources/Containerization/LinuxProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,21 @@ public final class LinuxProcess: Sendable {
var stdout: FileHandle?
var stderr: FileHandle?

func close() throws {
mutating func close() throws {
if let stdin {
try stdin.close()
stdin.readabilityHandler = nil
self.stdin = nil
}
if let stdout {
try stdout.close()
stdout.readabilityHandler = nil
self.stdout = nil
}
if let stderr {
try stderr.close()
stderr.readabilityHandler = nil
self.stderr = nil
}
}
}
Expand Down Expand Up @@ -194,6 +200,7 @@ extension LinuxProcess {
try handle.write(contentsOf: data)
} catch {
self.logger?.error("failed to write to stdin: \(error)")
return
}
}
}
Expand All @@ -208,7 +215,12 @@ extension LinuxProcess {
// as it always allocates. We can likely do the read loop ourselves
// with a buffer we allocate once on creation of the process.
do {
try stdout.writer.write(handle.availableData)
let data = handle.availableData
if data.isEmpty {
handles[1]?.readabilityHandler = nil
return
}
try stdout.writer.write(data)
} catch {
self.logger?.error("failed to write to stdout: \(error)")
}
Expand All @@ -218,7 +230,12 @@ extension LinuxProcess {
if let stderr = self.ioSetup.stderr {
handles[2]?.readabilityHandler = { handle in
do {
try stderr.writer.write(handle.availableData)
let data = handle.availableData
if data.isEmpty {
handles[2]?.readabilityHandler = nil
return
}
try stderr.writer.write(data)
} catch {
self.logger?.error("failed to write to stderr: \(error)")
}
Expand Down Expand Up @@ -334,5 +351,8 @@ extension LinuxProcess {
$0.stdinRelay?.cancel()
try $0.stdio.close()
}

// Finally, close our agent conn.
try await self.agent.close()
}
}