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:
@@ -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
+ }
}
}
