Skip to content

Commit 22ccc1b

Browse files
committed
Don't call s.jailer.Close() from multiple places
All cleanup logic is moved to cleanup(). StopVM() will call the method to make sure all resouces are deleted at the end of the request. monitorVMExit() will call the method in case the VM is terminated without having a StopVM request. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent ad4260f commit 22ccc1b

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

runtime/service.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ type service struct {
121121
config *config.Config
122122

123123
// vmReady is closed once CreateVM has been successfully called
124-
vmReady chan struct{}
125-
// vmExitErr is set with the error returned by machine.Wait(). It should
126-
// only be read after shimCtx.Done() is closed
127-
vmExitErr error
124+
vmReady chan struct{}
128125
vmStartOnce sync.Once
129126
agentClient taskAPI.TaskService
130127
eventBridgeClient eventbridge.Getter
@@ -134,6 +131,9 @@ type service struct {
134131
driveMountStubs []MountableStubDrive
135132
exitAfterAllTasksDeleted bool // exit the VM and shim when all tasks are deleted
136133

134+
cleanupErr error
135+
cleanupOnce sync.Once
136+
137137
machine *firecracker.Machine
138138
machineConfig *firecracker.Config
139139
vsockIOPortCount uint32
@@ -583,7 +583,6 @@ func (s *service) StopVM(requestCtx context.Context, request *proto.StopVMReques
583583
timeout = time.Duration(request.TimeoutSeconds) * time.Second
584584
}
585585

586-
defer s.shimCancel()
587586
err = s.waitVMReady()
588587
if err != nil {
589588
return nil, err
@@ -1178,12 +1177,9 @@ func (s *service) shutdown(
11781177
if err := s.machine.Wait(context.Background()); err != nil {
11791178
result = multierror.Append(result, err)
11801179
}
1181-
if err := s.jailer.Close(); err != nil {
1180+
if err := s.cleanup(); err != nil {
11821181
result = multierror.Append(result, err)
11831182
}
1184-
if result == nil {
1185-
return nil
1186-
}
11871183

11881184
if err := result.ErrorOrNil(); err != nil {
11891185
return status.Error(codes.Internal, fmt.Sprintf("the VMM was killed forcibly: %v", err))
@@ -1223,13 +1219,6 @@ func (s *service) shutdownLoop(
12231219
},
12241220
timeout: jailerStopTimeout,
12251221
},
1226-
{
1227-
name: "cancel the context",
1228-
shutdown: func() error {
1229-
s.shimCancel()
1230-
return nil
1231-
},
1232-
},
12331222
}
12341223

12351224
for _, action := range actions {
@@ -1305,26 +1294,38 @@ func (s *service) Cleanup(requestCtx context.Context) (*taskAPI.DeleteResponse,
13051294
}, nil
13061295
}
13071296

1308-
func (s *service) monitorVMExit() {
1309-
defer func() {
1297+
// cleanup resources
1298+
func (s *service) cleanup() error {
1299+
s.cleanupOnce.Do(func() {
1300+
var result *multierror.Error
13101301
// we ignore the error here due to cleanup will only succeed if the jailing
13111302
// process was killed via SIGKILL
13121303
if err := s.jailer.Close(); err != nil {
1304+
result = multierror.Append(result, err)
13131305
s.logger.WithError(err).Error("failed to close jailer")
13141306
}
13151307

1308+
if err := s.publishVMStop(); err != nil {
1309+
result = multierror.Append(result, err)
1310+
s.logger.WithError(err).Error("failed to publish stop VM event")
1311+
}
1312+
13161313
// once the VM shuts down, the shim should too
13171314
s.shimCancel()
1318-
}()
13191315

1316+
s.cleanupErr = result.ErrorOrNil()
1317+
})
1318+
return s.cleanupErr
1319+
}
1320+
1321+
// monitorVMExit watches the VM and cleanup resources when it terminates.
1322+
func (s *service) monitorVMExit() {
13201323
// Block until the VM exits
1321-
s.vmExitErr = s.machine.Wait(s.shimCtx)
1322-
if s.vmExitErr != nil && s.vmExitErr != context.Canceled {
1323-
s.logger.WithError(s.vmExitErr).Error("error returned from VM wait")
1324+
if err := s.machine.Wait(s.shimCtx); err != nil && err != context.Canceled {
1325+
s.logger.WithError(err).Error("error returned from VM wait")
13241326
}
13251327

1326-
publishErr := s.publishVMStop()
1327-
if publishErr != nil {
1328-
s.logger.WithError(publishErr).Error("failed to publish stop VM event")
1328+
if err := s.cleanup(); err != nil {
1329+
s.logger.WithError(err).Error("failed to clean up the VM")
13291330
}
13301331
}

0 commit comments

Comments
 (0)