From ed772cd8fe1c478318eb5b9622e022412b577df2 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 13 Oct 2020 12:17:29 +0200 Subject: [PATCH] [Ingest Manager] Change Sync/Close call order (#21735) [Ingest Manager] Change Sync/Close call order (#21735) --- .../pkg/artifact/install/tar/tar_installer.go | 14 +++++++++----- .../pkg/artifact/install/zip/zip_installer.go | 11 ++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go b/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go index b9e621c71ace..74a74e4c6bc5 100644 --- a/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go +++ b/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go @@ -99,15 +99,19 @@ func unpack(r io.Reader, dir string) error { } _, err = io.Copy(wf, tr) + + if err == nil { + // sometimes we try executing binary too fast and run into text file busy after unpacking + // syncing prevents this + if syncErr := wf.Sync(); syncErr != nil { + err = syncErr + } + } + if closeErr := wf.Close(); closeErr != nil && err == nil { err = closeErr } - // sometimes we try executing binary too fast and run into text file busy after unpacking - // syncing prevents this - if syncErr := wf.Sync(); syncErr != nil && err == nil { - err = syncErr - } if err != nil { return fmt.Errorf("TarInstaller: error writing to %s: %v", abs, err) } diff --git a/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go b/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go index 29cdb66f852c..b565f630a73e 100644 --- a/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go +++ b/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go @@ -105,18 +105,15 @@ func (i *Installer) unzip(artifactPath string) error { if closeErr := f.Close(); closeErr != nil { err = multierror.Append(err, closeErr) } - - // sometimes we try executing binary too fast and run into text file busy after unpacking - // syncing prevents this - if syncErr := f.Sync(); syncErr != nil { - err = multierror.Append(err, syncErr) - } - }() if _, err = io.Copy(f, rc); err != nil { return err } + + // sometimes we try executing binary too fast and run into text file busy after unpacking + // syncing prevents this + f.Sync() } return nil }