Skip to content
Open
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
43 changes: 27 additions & 16 deletions Sources/Containerization/LinuxContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -517,21 +517,18 @@ extension LinuxContainer {
}

let startedState = try state.startedState("stop")
let vm = startedState.vm

var firstError: Error?
do {
try await startedState.relayManager.stopAll()
} catch {
self.logger?.error("failed to stop relay manager: \(error)")
firstError = firstError ?? error
}

// It's possible the state of the vm is not in a great spot
// if the guest panicked or had any sort of bug/fault.
// First check if the vm is even still running, as trying to
// use a vsock handle like below here will cause NIO to
// fatalError because we'll get an EBADF.
if startedState.vm.state == .stopped {
state = .stopped
return
}

try await startedState.vm.withAgent { agent in
do {
try await vm.withAgent { agent in
// First, we need to stop any unix socket relays as this will
// keep the rootfs from being able to umount (EBUSY).
let sockets = self.config.sockets
Expand Down Expand Up @@ -564,15 +561,29 @@ extension LinuxContainer {
flags: 0
)
}
} catch {
self.logger?.error("failed during guest cleanup: \(error)")
firstError = firstError ?? error
}

// Lets free up the init procs resources, as this includes the open agent conn.
try? await startedState.process.delete()
do {
try await startedState.process.delete()
} catch {
self.logger?.error("failed to delete process: \(error)")
firstError = firstError ?? error
}

try await startedState.vm.stop()
do {
try await vm.stop()
state = .stopped
if let firstError {
throw firstError
}
} catch {
state.setErrored(error: error)
throw error
self.logger?.error("failed to stop VM: \(error)")
let finalError = firstError ?? error
state.setErrored(error: finalError)
throw finalError
}
}
}
Expand Down
34 changes: 24 additions & 10 deletions vminitd/Sources/vminitd/Server+GRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -555,18 +555,32 @@ extension Initd: Com_Apple_Containerization_Sandbox_V3_SandboxContextAsyncProvid
)
}

let ctr = try await self.state.get(container: request.containerID)
do {
let ctr = try await self.state.get(container: request.containerID)

// Are we trying to delete the container itself?
if request.id == request.containerID {
try await ctr.delete()
try await state.remove(container: request.id)
} else {
// Or just a single exec.
try await ctr.deleteExec(id: request.id)
}
// Are we trying to delete the container itself?
if request.id == request.containerID {
try await ctr.delete()
try await state.remove(container: request.id)
} else {
// Or just a single exec.
try await ctr.deleteExec(id: request.id)
}

return .init()
return .init()
} catch {
log.error(
"deleteProcess",
metadata: [
"id": "\(request.id)",
"containerID": "\(request.containerID)",
"error": "\(error)",
])
throw GRPCStatus(
code: .internalError,
message: "deleteProcess: \(error)"
)
}
}

func startProcess(
Expand Down