@@ -102,7 +102,8 @@ func NewProcess(
102
102
return nil , fmt .Errorf ("error stating kernel file: %w" , err )
103
103
}
104
104
105
- cmd := exec .Command (
105
+ cmd := exec .CommandContext (
106
+ ctx ,
106
107
"unshare" ,
107
108
"-m" ,
108
109
"--" ,
@@ -200,7 +201,7 @@ func (p *Process) configure(
200
201
if err != nil {
201
202
errMsg := fmt .Errorf ("error waiting for fc socket: %w" , err )
202
203
203
- fcStopErr := p .Stop ()
204
+ fcStopErr := p .Stop (startCtx )
204
205
205
206
return errors .Join (errMsg , fcStopErr )
206
207
}
@@ -226,7 +227,7 @@ func (p *Process) Create(
226
227
options .Stderr ,
227
228
)
228
229
if err != nil {
229
- fcStopErr := p .Stop ()
230
+ fcStopErr := p .Stop (ctx )
230
231
231
232
return errors .Join (fmt .Errorf ("error starting fc process: %w" , err ), fcStopErr )
232
233
}
@@ -275,7 +276,7 @@ func (p *Process) Create(
275
276
kernelArgs := args .String ()
276
277
err = p .client .setBootSource (ctx , kernelArgs , p .kernelPath )
277
278
if err != nil {
278
- fcStopErr := p .Stop ()
279
+ fcStopErr := p .Stop (ctx )
279
280
280
281
return errors .Join (fmt .Errorf ("error setting fc boot source config: %w" , err ), fcStopErr )
281
282
}
@@ -289,7 +290,7 @@ func (p *Process) Create(
289
290
290
291
err = p .client .setRootfsDrive (ctx , p .rootfsPath )
291
292
if err != nil {
292
- fcStopErr := p .Stop ()
293
+ fcStopErr := p .Stop (ctx )
293
294
294
295
return errors .Join (fmt .Errorf ("error setting fc drivers config: %w" , err ), fcStopErr )
295
296
}
@@ -298,23 +299,23 @@ func (p *Process) Create(
298
299
// Network
299
300
err = p .client .setNetworkInterface (ctx , p .slot .VpeerName (), p .slot .TapName (), p .slot .TapMAC ())
300
301
if err != nil {
301
- fcStopErr := p .Stop ()
302
+ fcStopErr := p .Stop (ctx )
302
303
303
304
return errors .Join (fmt .Errorf ("error setting fc network config: %w" , err ), fcStopErr )
304
305
}
305
306
telemetry .ReportEvent (ctx , "set fc network config" )
306
307
307
308
err = p .client .setMachineConfig (ctx , vCPUCount , memoryMB , hugePages )
308
309
if err != nil {
309
- fcStopErr := p .Stop ()
310
+ fcStopErr := p .Stop (ctx )
310
311
311
312
return errors .Join (fmt .Errorf ("error setting fc machine config: %w" , err ), fcStopErr )
312
313
}
313
314
telemetry .ReportEvent (ctx , "set fc machine config" )
314
315
315
316
err = p .client .startVM (ctx )
316
317
if err != nil {
317
- fcStopErr := p .Stop ()
318
+ fcStopErr := p .Stop (ctx )
318
319
319
320
return errors .Join (fmt .Errorf ("error starting fc: %w" , err ), fcStopErr )
320
321
}
@@ -340,7 +341,7 @@ func (p *Process) Resume(
340
341
nil ,
341
342
)
342
343
if err != nil {
343
- fcStopErr := p .Stop ()
344
+ fcStopErr := p .Stop (ctx )
344
345
345
346
return errors .Join (fmt .Errorf ("error starting fc process: %w" , err ), fcStopErr )
346
347
}
@@ -359,21 +360,21 @@ func (p *Process) Resume(
359
360
snapfile ,
360
361
)
361
362
if err != nil {
362
- fcStopErr := p .Stop ()
363
+ fcStopErr := p .Stop (ctx )
363
364
364
365
return errors .Join (fmt .Errorf ("error loading snapshot: %w" , err ), fcStopErr )
365
366
}
366
367
367
368
err = p .client .resumeVM (ctx )
368
369
if err != nil {
369
- fcStopErr := p .Stop ()
370
+ fcStopErr := p .Stop (ctx )
370
371
371
372
return errors .Join (fmt .Errorf ("error resuming vm: %w" , err ), fcStopErr )
372
373
}
373
374
374
375
err = p .client .setMmds (ctx , mmdsMetadata )
375
376
if err != nil {
376
- fcStopErr := p .Stop ()
377
+ fcStopErr := p .Stop (ctx )
377
378
378
379
return errors .Join (fmt .Errorf ("error setting mmds: %w" , err ), fcStopErr )
379
380
}
@@ -397,8 +398,8 @@ func (p *Process) Pid() (int, error) {
397
398
398
399
// getProcessState returns the state of the process.
399
400
// 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 ()
402
403
if err != nil {
403
404
return "" , err
404
405
}
@@ -408,12 +409,12 @@ func getProcessState(pid int) (string, error) {
408
409
return state , nil
409
410
}
410
411
411
- func (p * Process ) Stop () error {
412
+ func (p * Process ) Stop (ctx context. Context ) error {
412
413
if p .cmd .Process == nil {
413
414
return fmt .Errorf ("fc process not started" )
414
415
}
415
416
416
- state , err := getProcessState (p .cmd .Process .Pid )
417
+ state , err := getProcessState (ctx , p .cmd .Process .Pid )
417
418
if err != nil {
418
419
zap .L ().Warn ("failed to get fc process state" , zap .Error (err ), logger .WithSandboxID (p .files .SandboxID ))
419
420
} else if state == "D" {
@@ -436,7 +437,7 @@ func (p *Process) Stop() error {
436
437
zap .L ().Info ("sent SIGKILL to fc process because it was not responding to SIGTERM for 10 seconds" , logger .WithSandboxID (p .files .SandboxID ))
437
438
}
438
439
439
- state , err := getProcessState (p .cmd .Process .Pid )
440
+ state , err := getProcessState (ctx , p .cmd .Process .Pid )
440
441
if err != nil {
441
442
zap .L ().Warn ("failed to get fc process state after sending SIGKILL" , zap .Error (err ), logger .WithSandboxID (p .files .SandboxID ))
442
443
} else if state == "D" {
0 commit comments