Skip to content

Commit 1388dbc

Browse files
committed
Remove shutdown sleep, let launchd erminate process
1 parent 87a4290 commit 1388dbc

File tree

2 files changed

+14
-57
lines changed

2 files changed

+14
-57
lines changed

Sources/Services/ContainerAPIService/Containers/ContainersService.swift

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,6 @@ public actor ContainersService {
9797
message: "failed to find runtime plugin \(config.runtimeHandler)"
9898
)
9999
}
100-
try Self.registerService(
101-
plugin: plugin,
102-
loader: loader,
103-
configuration: config,
104-
path: dir
105-
)
106100
} catch {
107101
try? FileManager.default.removeItem(at: dir)
108102
log.warning("failed to load container bundle at \(dir.path)")
@@ -185,13 +179,6 @@ public actor ContainersService {
185179
try bundle.setContainerRootFs(cloning: imageFs)
186180
try bundle.write(filename: "options.json", value: options)
187181

188-
try Self.registerService(
189-
plugin: runtimePlugin,
190-
loader: self.pluginLoader,
191-
configuration: configuration,
192-
path: path
193-
)
194-
195182
let snapshot = ContainerSnapshot(
196183
configuration: configuration,
197184
status: .stopped,
@@ -226,22 +213,6 @@ public actor ContainersService {
226213
let path = self.containerRoot.appendingPathComponent(id)
227214
let bundle = ContainerClient.Bundle(path: path)
228215
let config = try bundle.configuration
229-
let label = Self.fullLaunchdServiceLabel(
230-
runtimeName: config.runtimeHandler,
231-
instanceId: id
232-
)
233-
234-
let isRegistered = try ServiceManager.isRegistered(fullServiceLabel: label)
235-
236-
if isRegistered {
237-
// Stale service exists
238-
self.log.warning("Found stale launchd service for \(id), cleaning up")
239-
try? ServiceManager.kill(fullServiceLabel: label, signal: SIGKILL)
240-
try? await Task.sleep(for: .milliseconds(100))
241-
try? ServiceManager.deregister(fullServiceLabel: label)
242-
}
243-
244-
// Always register service
245216
try Self.registerService(
246217
plugin: self.runtimePlugins.first { $0.name == config.runtimeHandler }!,
247218
loader: self.pluginLoader,
@@ -483,28 +454,23 @@ public actor ContainersService {
483454

484455
await self.exitMonitor.stopTracking(id: id)
485456

486-
// Shutdown the runtime helper immediately to prevent reconnection
487-
do {
488-
self.log.info("Shutting down sandbox service for \(id)")
457+
// Shutdown and deregister the sandbox service
458+
self.log.info("Shutting down sandbox service for \(id)")
489459

490-
let path = self.containerRoot.appendingPathComponent(id)
491-
let bundle = ContainerClient.Bundle(path: path)
492-
let config = try bundle.configuration
493-
let label = Self.fullLaunchdServiceLabel(
494-
runtimeName: config.runtimeHandler,
495-
instanceId: id
496-
)
460+
let path = self.containerRoot.appendingPathComponent(id)
461+
let bundle = ContainerClient.Bundle(path: path)
462+
let config = try bundle.configuration
463+
let label = Self.fullLaunchdServiceLabel(
464+
runtimeName: config.runtimeHandler,
465+
instanceId: id
466+
)
497467

498-
let client = try state.getClient()
499-
try? await client.shutdown()
500-
try? ServiceManager.kill(fullServiceLabel: label, signal: SIGKILL)
501-
try? await Task.sleep(for: .milliseconds(100))
468+
let client = try state.getClient()
469+
try await client.shutdown()
502470

503-
try ServiceManager.deregister(fullServiceLabel: label)
504-
self.log.info("Cleaned up sandbox service for \(id)")
505-
} catch {
506-
self.log.error("failed to cleanup sandbox service for \(id): \(error)")
507-
}
471+
// Deregister the service, launchd will terminate the process
472+
try ServiceManager.deregister(fullServiceLabel: label)
473+
self.log.info("Deregistered sandbox service for \(id)")
508474

509475
state.snapshot.status = .stopped
510476
state.snapshot.networks = []

Sources/Services/ContainerSandboxService/SandboxService.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,6 @@ public actor SandboxService {
272272
case .created, .stopped(_), .stopping:
273273
await self.setState(.shuttingDown)
274274

275-
Task {
276-
do {
277-
try await Task.sleep(for: .seconds(5))
278-
} catch {
279-
self.log.error("failed to sleep before shutting down SandboxService: \(error)")
280-
}
281-
self.log.info("Shutting down SandboxService")
282-
exit(0)
283-
}
284275
default:
285276
throw ContainerizationError(
286277
.invalidState,

0 commit comments

Comments
 (0)