From 94e5c211aed2c511b8b9e7fd2761db1c89b105b0 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Mon, 12 Oct 2020 10:50:48 +0200 Subject: [PATCH] syncing unpacked files --- .../pkg/artifact/install/tar/tar_installer.go | 6 ++++++ .../pkg/artifact/install/zip/zip_installer.go | 11 +++++++++-- 2 files changed, 15 insertions(+), 2 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 5c7f0f593a3c..b9e621c71ace 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 @@ -102,6 +102,12 @@ func unpack(r io.Reader, dir string) error { 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 ffc90f2dce8a..29cdb66f852c 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 @@ -102,9 +102,16 @@ func (i *Installer) unzip(artifactPath string) error { return err } defer func() { - if cerr := f.Close(); cerr != nil { - err = multierror.Append(err, cerr) + 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 {