Skip to content

Commit f1ab7d0

Browse files
committed
add more context to firecracker calls
1 parent d61fd01 commit f1ab7d0

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

packages/orchestrator/internal/sandbox/fc/process.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ func NewProcess(
102102
return nil, fmt.Errorf("error stating kernel file: %w", err)
103103
}
104104

105-
cmd := exec.Command(
105+
cmd := exec.CommandContext(
106+
ctx,
106107
"unshare",
107108
"-m",
108109
"--",
@@ -200,7 +201,7 @@ func (p *Process) configure(
200201
if err != nil {
201202
errMsg := fmt.Errorf("error waiting for fc socket: %w", err)
202203

203-
fcStopErr := p.Stop()
204+
fcStopErr := p.Stop(startCtx)
204205

205206
return errors.Join(errMsg, fcStopErr)
206207
}
@@ -226,7 +227,7 @@ func (p *Process) Create(
226227
options.Stderr,
227228
)
228229
if err != nil {
229-
fcStopErr := p.Stop()
230+
fcStopErr := p.Stop(ctx)
230231

231232
return errors.Join(fmt.Errorf("error starting fc process: %w", err), fcStopErr)
232233
}
@@ -275,7 +276,7 @@ func (p *Process) Create(
275276
kernelArgs := args.String()
276277
err = p.client.setBootSource(ctx, kernelArgs, p.kernelPath)
277278
if err != nil {
278-
fcStopErr := p.Stop()
279+
fcStopErr := p.Stop(ctx)
279280

280281
return errors.Join(fmt.Errorf("error setting fc boot source config: %w", err), fcStopErr)
281282
}
@@ -289,7 +290,7 @@ func (p *Process) Create(
289290

290291
err = p.client.setRootfsDrive(ctx, p.rootfsPath)
291292
if err != nil {
292-
fcStopErr := p.Stop()
293+
fcStopErr := p.Stop(ctx)
293294

294295
return errors.Join(fmt.Errorf("error setting fc drivers config: %w", err), fcStopErr)
295296
}
@@ -298,23 +299,23 @@ func (p *Process) Create(
298299
// Network
299300
err = p.client.setNetworkInterface(ctx, p.slot.VpeerName(), p.slot.TapName(), p.slot.TapMAC())
300301
if err != nil {
301-
fcStopErr := p.Stop()
302+
fcStopErr := p.Stop(ctx)
302303

303304
return errors.Join(fmt.Errorf("error setting fc network config: %w", err), fcStopErr)
304305
}
305306
telemetry.ReportEvent(ctx, "set fc network config")
306307

307308
err = p.client.setMachineConfig(ctx, vCPUCount, memoryMB, hugePages)
308309
if err != nil {
309-
fcStopErr := p.Stop()
310+
fcStopErr := p.Stop(ctx)
310311

311312
return errors.Join(fmt.Errorf("error setting fc machine config: %w", err), fcStopErr)
312313
}
313314
telemetry.ReportEvent(ctx, "set fc machine config")
314315

315316
err = p.client.startVM(ctx)
316317
if err != nil {
317-
fcStopErr := p.Stop()
318+
fcStopErr := p.Stop(ctx)
318319

319320
return errors.Join(fmt.Errorf("error starting fc: %w", err), fcStopErr)
320321
}
@@ -340,7 +341,7 @@ func (p *Process) Resume(
340341
nil,
341342
)
342343
if err != nil {
343-
fcStopErr := p.Stop()
344+
fcStopErr := p.Stop(ctx)
344345

345346
return errors.Join(fmt.Errorf("error starting fc process: %w", err), fcStopErr)
346347
}
@@ -359,21 +360,21 @@ func (p *Process) Resume(
359360
snapfile,
360361
)
361362
if err != nil {
362-
fcStopErr := p.Stop()
363+
fcStopErr := p.Stop(ctx)
363364

364365
return errors.Join(fmt.Errorf("error loading snapshot: %w", err), fcStopErr)
365366
}
366367

367368
err = p.client.resumeVM(ctx)
368369
if err != nil {
369-
fcStopErr := p.Stop()
370+
fcStopErr := p.Stop(ctx)
370371

371372
return errors.Join(fmt.Errorf("error resuming vm: %w", err), fcStopErr)
372373
}
373374

374375
err = p.client.setMmds(ctx, mmdsMetadata)
375376
if err != nil {
376-
fcStopErr := p.Stop()
377+
fcStopErr := p.Stop(ctx)
377378

378379
return errors.Join(fmt.Errorf("error setting mmds: %w", err), fcStopErr)
379380
}
@@ -397,8 +398,8 @@ func (p *Process) Pid() (int, error) {
397398

398399
// getProcessState returns the state of the process.
399400
// It's used to check if the process is in the D state, because gopsutil doesn't show that.
400-
func getProcessState(pid int) (string, error) {
401-
cmd, err := exec.Command("ps", "-o", "stat=", "-p", fmt.Sprint(pid)).Output()
401+
func getProcessState(ctx context.Context, pid int) (string, error) {
402+
cmd, err := exec.CommandContext(ctx, "ps", "-o", "stat=", "-p", fmt.Sprint(pid)).Output()
402403
if err != nil {
403404
return "", err
404405
}
@@ -408,12 +409,12 @@ func getProcessState(pid int) (string, error) {
408409
return state, nil
409410
}
410411

411-
func (p *Process) Stop() error {
412+
func (p *Process) Stop(ctx context.Context) error {
412413
if p.cmd.Process == nil {
413414
return fmt.Errorf("fc process not started")
414415
}
415416

416-
state, err := getProcessState(p.cmd.Process.Pid)
417+
state, err := getProcessState(ctx, p.cmd.Process.Pid)
417418
if err != nil {
418419
zap.L().Warn("failed to get fc process state", zap.Error(err), logger.WithSandboxID(p.files.SandboxID))
419420
} else if state == "D" {
@@ -436,7 +437,7 @@ func (p *Process) Stop() error {
436437
zap.L().Info("sent SIGKILL to fc process because it was not responding to SIGTERM for 10 seconds", logger.WithSandboxID(p.files.SandboxID))
437438
}
438439

439-
state, err := getProcessState(p.cmd.Process.Pid)
440+
state, err := getProcessState(ctx, p.cmd.Process.Pid)
440441
if err != nil {
441442
zap.L().Warn("failed to get fc process state after sending SIGKILL", zap.Error(err), logger.WithSandboxID(p.files.SandboxID))
442443
} else if state == "D" {

packages/orchestrator/internal/sandbox/sandbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ func (s *Sandbox) Stop(ctx context.Context) error {
588588
// Stop the health checks before stopping the sandbox
589589
s.Checks.Stop()
590590

591-
fcStopErr := s.process.Stop()
591+
fcStopErr := s.process.Stop(ctx)
592592
if fcStopErr != nil {
593593
errs = append(errs, fmt.Errorf("failed to stop FC: %w", fcStopErr))
594594
}

0 commit comments

Comments
 (0)