LinuxProcess: Resource adjustments (#33)

- The kqueue handlers for stdio didn't have logic to exit on zero byte
reads.
- The agent wasn't getting closed explicitly in .delete()
- In LinuxContainer we should call delete just for sanity, if for
nothing more than ensuring the agent vsock fd is closed.

Signed-off-by: Danny Canter <danny_canter@apple.com>
This commit is contained in:
Danny Canter
2025-06-08 02:07:23 -07:00
committed by GitHub
parent 0bd526053c
commit fef518e1c3
2 changed files with 26 additions and 3 deletions
@@ -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()
}
+23 -3
View File
@@ -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
}
}
}
@@ -194,6 +200,7 @@ extension LinuxProcess {
try handle.write(contentsOf: data)
} catch {
self.logger?.error("failed to write to stdin: \(error)")
return
}
}
}
@@ -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)")
}
@@ -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)")
}
@@ -334,5 +351,8 @@ extension LinuxProcess {
$0.stdinRelay?.cancel()
try $0.stdio.close()
}
// Finally, close our agent conn.
try await self.agent.close()
}
}