runtime: close span before return from function in case of error

Return before closing span will cause invalid spans, so span should
be closed before function return.

Fixes: #3424

Signed-off-by: bin <bin@hyper.sh>
This commit is contained in:
bin 2022-01-11 19:45:41 +08:00
parent 7d1a956471
commit 85f5ae190e

View File

@ -606,11 +606,16 @@ func (c *Container) unmountHostMounts(ctx context.Context) error {
span, ctx := katatrace.Trace(ctx, c.Logger(), "unmountHostMounts", containerTracingTags, map[string]string{"container_id": c.id})
defer span.End()
for _, m := range c.mounts {
if m.HostPath != "" {
unmountFunc := func(m Mount) (err error) {
span, _ := katatrace.Trace(ctx, c.Logger(), "unmount", containerTracingTags, map[string]string{"container_id": c.id, "host-path": m.HostPath})
defer func() {
if err != nil {
katatrace.AddTags(span, "error", err)
}
span.End()
}()
if err := syscall.Unmount(m.HostPath, syscall.MNT_DETACH|UmountNoFollow); err != nil {
if err = syscall.Unmount(m.HostPath, syscall.MNT_DETACH|UmountNoFollow); err != nil {
c.Logger().WithFields(logrus.Fields{
"host-path": m.HostPath,
"error": err,
@ -631,8 +636,14 @@ func (c *Container) unmountHostMounts(ctx context.Context) error {
syscall.Rmdir(m.HostPath)
}
}
return nil
}
span.End()
for _, m := range c.mounts {
if m.HostPath != "" {
if err := unmountFunc(m); err != nil {
return err
}
}
}