Skip to content

Commit fef518e

Browse files
authored
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>
1 parent 0bd5260 commit fef518e

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

Sources/Containerization/LinuxContainer.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,9 @@ extension LinuxContainer {
645645
)
646646
}
647647

648+
// Lets free up the init procs resources, as this includes the open agent conn.
649+
try? await startedState.process.delete()
650+
648651
try await startedState.vm.stop()
649652
try state.stopped()
650653
}

Sources/Containerization/LinuxProcess.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,21 @@ public final class LinuxProcess: Sendable {
7171
var stdout: FileHandle?
7272
var stderr: FileHandle?
7373

74-
func close() throws {
74+
mutating func close() throws {
7575
if let stdin {
7676
try stdin.close()
77+
stdin.readabilityHandler = nil
78+
self.stdin = nil
7779
}
7880
if let stdout {
7981
try stdout.close()
82+
stdout.readabilityHandler = nil
83+
self.stdout = nil
8084
}
8185
if let stderr {
8286
try stderr.close()
87+
stderr.readabilityHandler = nil
88+
self.stderr = nil
8389
}
8490
}
8591
}
@@ -194,6 +200,7 @@ extension LinuxProcess {
194200
try handle.write(contentsOf: data)
195201
} catch {
196202
self.logger?.error("failed to write to stdin: \(error)")
203+
return
197204
}
198205
}
199206
}
@@ -208,7 +215,12 @@ extension LinuxProcess {
208215
// as it always allocates. We can likely do the read loop ourselves
209216
// with a buffer we allocate once on creation of the process.
210217
do {
211-
try stdout.writer.write(handle.availableData)
218+
let data = handle.availableData
219+
if data.isEmpty {
220+
handles[1]?.readabilityHandler = nil
221+
return
222+
}
223+
try stdout.writer.write(data)
212224
} catch {
213225
self.logger?.error("failed to write to stdout: \(error)")
214226
}
@@ -218,7 +230,12 @@ extension LinuxProcess {
218230
if let stderr = self.ioSetup.stderr {
219231
handles[2]?.readabilityHandler = { handle in
220232
do {
221-
try stderr.writer.write(handle.availableData)
233+
let data = handle.availableData
234+
if data.isEmpty {
235+
handles[2]?.readabilityHandler = nil
236+
return
237+
}
238+
try stderr.writer.write(data)
222239
} catch {
223240
self.logger?.error("failed to write to stderr: \(error)")
224241
}
@@ -334,5 +351,8 @@ extension LinuxProcess {
334351
$0.stdinRelay?.cancel()
335352
try $0.stdio.close()
336353
}
354+
355+
// Finally, close our agent conn.
356+
try await self.agent.close()
337357
}
338358
}

0 commit comments

Comments
 (0)