Skip to content

Commit b348216

Browse files
committed
Fix firecracker process cleanup and shutdown latency
1 parent f3b49cb commit b348216

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/hypervisor/firecracker/process.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ func (s *Starter) startProcess(_ context.Context, p *paths.Paths, version string
148148
}
149149

150150
if err := waitForSocket(socketPath, socketWaitTimeout); err != nil {
151+
if cmd.Process != nil {
152+
_ = cmd.Process.Kill()
153+
_ = cmd.Wait()
154+
}
151155
if data, readErr := os.ReadFile(vmmLogPath); readErr == nil && len(data) > 0 {
152156
return 0, fmt.Errorf("%w; vmm.log: %s", err, string(data))
153157
}

lib/instances/standby.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ func (m *manager) shutdownHypervisor(ctx context.Context, inst *Instance) error
190190

191191
// Wait for process to exit
192192
if inst.HypervisorPID != nil {
193-
if !WaitForProcessExit(*inst.HypervisorPID, 2*time.Second) {
194-
log.WarnContext(ctx, "hypervisor did not exit gracefully in time, force killing process", "instance_id", inst.Id, "pid", *inst.HypervisorPID)
193+
forceKill := func() error {
195194
if err := syscall.Kill(*inst.HypervisorPID, syscall.SIGKILL); err != nil && err != syscall.ESRCH {
196195
return fmt.Errorf("force kill hypervisor pid %d: %w", *inst.HypervisorPID, err)
197196
}
@@ -202,6 +201,19 @@ func (m *manager) shutdownHypervisor(ctx context.Context, inst *Instance) error
202201
return fmt.Errorf("hypervisor pid %d did not exit after SIGKILL", *inst.HypervisorPID)
203202
}
204203
}
204+
return nil
205+
}
206+
207+
if shutdownErr == hypervisor.ErrNotSupported {
208+
log.DebugContext(ctx, "hypervisor does not support graceful shutdown, force killing process", "instance_id", inst.Id, "pid", *inst.HypervisorPID)
209+
if err := forceKill(); err != nil {
210+
return err
211+
}
212+
} else if !WaitForProcessExit(*inst.HypervisorPID, 2*time.Second) {
213+
log.WarnContext(ctx, "hypervisor did not exit gracefully in time, force killing process", "instance_id", inst.Id, "pid", *inst.HypervisorPID)
214+
if err := forceKill(); err != nil {
215+
return err
216+
}
205217
} else {
206218
log.DebugContext(ctx, "hypervisor shutdown gracefully", "instance_id", inst.Id, "pid", *inst.HypervisorPID)
207219
}

0 commit comments

Comments
 (0)