Skip to content

Commit b75b80f

Browse files
adityaramanielijah-wright
authored andcommitted
Better IO handling
Signed-off-by: Aditya Ramani <a_ramani@apple.com>
1 parent c528567 commit b75b80f

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

Sources/Integration/ProcessTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ extension IntegrationSuite {
204204

205205
let status = try await exec.wait()
206206
if status != 0 {
207-
throw IntegrationError.assert(msg: "process \(idx) status for \(status) != 0")
207+
throw IntegrationError.assert(msg: "process \(idx) status \(status) != 0")
208208
}
209209
var hasher = SHA256()
210210
hasher.update(data: buffer.data)

vminitd/Sources/vminitd/ManagedProcess.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ final class ManagedProcess: Sendable {
132132
extension ManagedProcess {
133133
func start() throws -> Int32 {
134134
try self.lock.withLock {
135-
log.debug("starting managed process")
135+
log.debug(
136+
"starting managed process",
137+
metadata: [
138+
"id": "\(id)"
139+
])
136140

137141
// Start the underlying process.
138142
try process.start()
@@ -155,7 +159,8 @@ extension ManagedProcess {
155159
log.debug(
156160
"started managed process",
157161
metadata: [
158-
"pid": "\(i)"
162+
"pid": "\(i)",
163+
"id": "\(id)",
159164
])
160165

161166
return i

vminitd/Sources/vminitd/ProcessSupervisor.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,15 @@ actor ProcessSupervisor {
6464
let exited = Reaper.reap()
6565
self.log?.debug("finished wait4 of \(exited.count) processes")
6666

67-
for proc in processes {
68-
let pid = proc.pid
69-
self.log?.debug("checking for exit of managed process", metadata: ["pid": "\(pid)", "exits": "\(exited)"])
67+
self.log?.debug("checking for exit of managed process", metadata: ["exits": "\(exited)", "processes": "\(processes.count)"])
68+
let exitedProcesses = self.processes.filter { proc in
69+
exited.contains { pid, _ in
70+
proc.pid == pid
71+
}
72+
}
7073

74+
for proc in exitedProcesses {
75+
let pid = proc.pid
7176
if pid <= 0 {
7277
continue
7378
}
@@ -80,7 +85,6 @@ actor ProcessSupervisor {
8085
"status": "\(status)",
8186
"count": "\(processes.count - 1)",
8287
])
83-
8488
proc.setExit(status)
8589
self.processes.removeAll(where: { $0.pid == pid })
8690
}
@@ -95,7 +99,6 @@ actor ProcessSupervisor {
9599

96100
do {
97101
self.processes.append(process)
98-
99102
return try process.start()
100103
} catch {
101104
self.log?.error("process start failed \(error)", metadata: ["process-id": "\(process.id)"])

vminitd/Sources/vminitd/StandardIO.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ final class StandardIO: ManagedProcess.IO & Sendable {
7878
port: stdinPort,
7979
cid: VsockType.hostCID
8080
)
81-
let stdinSocket = try Socket(type: type)
81+
let stdinSocket = try Socket(type: type, closeOnDeinit: false)
8282
try stdinSocket.connect()
8383
self.stdinSocket = stdinSocket
8484

@@ -93,7 +93,8 @@ final class StandardIO: ManagedProcess.IO & Sendable {
9393
port: stdoutPort,
9494
cid: VsockType.hostCID
9595
)
96-
let stdoutSocket = try Socket(type: type)
96+
// These fd's get closed when cleanupRelay is called
97+
let stdoutSocket = try Socket(type: type, closeOnDeinit: false)
9798
try stdoutSocket.connect()
9899
self.stdoutSocket = stdoutSocket
99100

@@ -108,7 +109,7 @@ final class StandardIO: ManagedProcess.IO & Sendable {
108109
port: stderrPort,
109110
cid: VsockType.hostCID
110111
)
111-
let stderrSocket = try Socket(type: type)
112+
let stderrSocket = try Socket(type: type, closeOnDeinit: false)
112113
try stderrSocket.connect()
113114
self.stderrSocket = stderrSocket
114115

vminitd/Sources/vminitd/TerminalIO.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ final class TerminalIO: ManagedProcess.IO & Sendable {
6262
port: stdinPort,
6363
cid: VsockType.hostCID
6464
)
65-
let stdinSocket = try Socket(type: type)
65+
let stdinSocket = try Socket(type: type, closeOnDeinit: false)
6666
try stdinSocket.connect()
6767
self.stdinSocket = stdinSocket
6868

@@ -77,7 +77,7 @@ final class TerminalIO: ManagedProcess.IO & Sendable {
7777
port: stdoutPort,
7878
cid: VsockType.hostCID
7979
)
80-
let stdoutSocket = try Socket(type: type)
80+
let stdoutSocket = try Socket(type: type, closeOnDeinit: false)
8181
try stdoutSocket.connect()
8282
self.stdoutSocket = stdoutSocket
8383

0 commit comments

Comments
 (0)