Skip to content

Commit c8a4cb7

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

File tree

2 files changed

+16
-63
lines changed

2 files changed

+16
-63
lines changed

Sources/Services/ContainerAPIService/Containers/ContainersService.swift

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,12 @@ public actor ContainersService {
9090
)
9191
)
9292
results[config.id] = state
93-
let plugin = runtimePlugins.first { $0.name == config.runtimeHandler }
94-
guard let plugin else {
93+
guard runtimePlugins.first(where: { $0.name == config.runtimeHandler }) != nil else {
9594
throw ContainerizationError(
9695
.internalError,
9796
message: "failed to find runtime plugin \(config.runtimeHandler)"
9897
)
9998
}
100-
try Self.registerService(
101-
plugin: plugin,
102-
loader: loader,
103-
configuration: config,
104-
path: dir
105-
)
10699
} catch {
107100
try? FileManager.default.removeItem(at: dir)
108101
log.warning("failed to load container bundle at \(dir.path)")
@@ -159,10 +152,7 @@ public actor ContainersService {
159152
)
160153
}
161154

162-
let runtimePlugin = self.runtimePlugins.filter {
163-
$0.name == configuration.runtimeHandler
164-
}.first
165-
guard let runtimePlugin else {
155+
guard self.runtimePlugins.first(where: { $0.name == configuration.runtimeHandler }) != nil else {
166156
throw ContainerizationError(
167157
.notFound,
168158
message: "unable to locate runtime plugin \(configuration.runtimeHandler)"
@@ -185,13 +175,6 @@ public actor ContainersService {
185175
try bundle.setContainerRootFs(cloning: imageFs)
186176
try bundle.write(filename: "options.json", value: options)
187177

188-
try Self.registerService(
189-
plugin: runtimePlugin,
190-
loader: self.pluginLoader,
191-
configuration: configuration,
192-
path: path
193-
)
194-
195178
let snapshot = ContainerSnapshot(
196179
configuration: configuration,
197180
status: .stopped,
@@ -226,22 +209,6 @@ public actor ContainersService {
226209
let path = self.containerRoot.appendingPathComponent(id)
227210
let bundle = ContainerClient.Bundle(path: path)
228211
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
245212
try Self.registerService(
246213
plugin: self.runtimePlugins.first { $0.name == config.runtimeHandler }!,
247214
loader: self.pluginLoader,
@@ -483,28 +450,23 @@ public actor ContainersService {
483450

484451
await self.exitMonitor.stopTracking(id: id)
485452

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

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-
)
456+
let path = self.containerRoot.appendingPathComponent(id)
457+
let bundle = ContainerClient.Bundle(path: path)
458+
let config = try bundle.configuration
459+
let label = Self.fullLaunchdServiceLabel(
460+
runtimeName: config.runtimeHandler,
461+
instanceId: id
462+
)
497463

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))
464+
let client = try state.getClient()
465+
try await client.shutdown()
502466

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-
}
467+
// Deregister the service, launchd will terminate the process
468+
try ServiceManager.deregister(fullServiceLabel: label)
469+
self.log.info("Deregistered sandbox service for \(id)")
508470

509471
state.snapshot.status = .stopped
510472
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)