Skip to content

Commit 028fea1

Browse files
committed
pkg/hostagent: Add HostAgent.onCloseMu
- Add `HostAgent.cleanUp()` - use `HostAgent.onCloseMu` in `HostAgent.close()` Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
1 parent f47b60e commit 028fea1

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

pkg/hostagent/hostagent.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ type HostAgent struct {
6060
portForwarder *portForwarder // legacy SSH port forwarder
6161
grpcPortForwarder *portfwd.Forwarder
6262

63-
onClose []func() error // LIFO
63+
onClose []func() error // LIFO
64+
onCloseMu sync.Mutex
6465

6566
driver driver.Driver
6667
signalCh chan os.Signal
@@ -502,7 +503,7 @@ func (a *HostAgent) startHostAgentRoutines(ctx context.Context) error {
502503
}
503504
logrus.Info(msg)
504505
}
505-
a.onClose = append(a.onClose, func() error {
506+
a.cleanUp(func() error {
506507
logrus.Debugf("shutting down the SSH master")
507508
if exitMasterErr := ssh.ExitMaster(a.instSSHAddress, a.sshLocalPort, a.sshConfig); exitMasterErr != nil {
508509
logrus.WithError(exitMasterErr).Warn("failed to exit SSH master")
@@ -531,7 +532,7 @@ sudo chown -R "${USER}" /run/host-services`
531532
if err != nil {
532533
errs = append(errs, err)
533534
}
534-
a.onClose = append(a.onClose, func() error {
535+
a.cleanUp(func() error {
535536
var unmountErrs []error
536537
for _, m := range mounts {
537538
if unmountErr := m.close(); unmountErr != nil {
@@ -542,7 +543,7 @@ sudo chown -R "${USER}" /run/host-services`
542543
})
543544
}
544545
if len(a.instConfig.AdditionalDisks) > 0 {
545-
a.onClose = append(a.onClose, func() error {
546+
a.cleanUp(func() error {
546547
var unlockErrs []error
547548
for _, d := range a.instConfig.AdditionalDisks {
548549
disk, inspectErr := store.InspectDisk(d.Name)
@@ -601,7 +602,7 @@ sudo chown -R "${USER}" /run/host-services`
601602
errs = append(errs, err)
602603
}
603604
}
604-
a.onClose = append(a.onClose, func() error {
605+
a.cleanUp(func() error {
605606
var rmErrs []error
606607
for _, rule := range a.instConfig.CopyToHost {
607608
if rule.DeleteOnStop {
@@ -616,7 +617,16 @@ sudo chown -R "${USER}" /run/host-services`
616617
return errors.Join(errs...)
617618
}
618619

620+
// cleanUp registers a cleanup function to be called when the host agent is stopped.
621+
func (a *HostAgent) cleanUp(fn func() error) {
622+
a.onCloseMu.Lock()
623+
defer a.onCloseMu.Unlock()
624+
a.onClose = append(a.onClose, fn)
625+
}
626+
619627
func (a *HostAgent) close() error {
628+
a.onCloseMu.Lock()
629+
defer a.onCloseMu.Unlock()
620630
logrus.Infof("Shutting down the host agent")
621631
var errs []error
622632
for i := len(a.onClose) - 1; i >= 0; i-- {
@@ -645,7 +655,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
645655
localUnix := filepath.Join(a.instDir, filenames.GuestAgentSock)
646656
remoteUnix := "/run/lima-guestagent.sock"
647657

648-
a.onClose = append(a.onClose, func() error {
658+
a.cleanUp(func() error {
649659
logrus.Debugf("Stop forwarding unix sockets")
650660
var errs []error
651661
for _, rule := range a.instConfig.PortForwards {

0 commit comments

Comments
 (0)