Make pid an optional (#372)

Makes `pid` an optional to avoid killing the process with pid 0.
This commit is contained in:
Dmitry Kovba
2025-10-29 16:31:06 -07:00
committed by GitHub
parent 5696732315
commit 8682365800
3 changed files with 10 additions and 7 deletions
@@ -30,7 +30,7 @@ actor ManagedContainer {
private let bundle: ContainerizationOCI.Bundle
private var execs: [String: ManagedProcess] = [:]
var pid: Int32 {
var pid: Int32? {
self.initProcess.pid
}
+8 -4
View File
@@ -49,7 +49,7 @@ final class ManagedProcess: Sendable {
let io: IO
var waiters: [CheckedContinuation<ExitStatus, Never>] = []
var exitStatus: ExitStatus? = nil
var pid: Int32 = 0
var pid: Int32?
}
private static let ackPid = "AckPid"
@@ -67,7 +67,7 @@ final class ManagedProcess: Sendable {
private let bundle: ContainerizationOCI.Bundle
private let cgroupManager: Cgroup2Manager?
var pid: Int32 {
var pid: Int32? {
self.state.withLock {
$0.pid
}
@@ -286,12 +286,16 @@ extension ManagedProcess {
func kill(_ signal: Int32) throws {
try self.state.withLock {
guard let pid = $0.pid else {
throw ContainerizationError(.invalidState, message: "process PID is required")
}
guard $0.exitStatus == nil else {
return
}
self.log.info("sending signal \(signal) to process \($0.pid)")
guard Foundation.kill($0.pid, signal) == 0 else {
self.log.info("sending signal \(signal) to process \(pid)")
guard Foundation.kill(pid, signal) == 0 else {
throw POSIXError.fromErrno()
}
}
@@ -80,8 +80,7 @@ actor ProcessSupervisor {
}
for proc in exitedProcesses {
let pid = proc.pid
if pid <= 0 {
guard let pid = proc.pid else {
continue
}