Add binary removal delay on Windows to avoid access denied (#2351)

Co-authored-by: Polina Sokolova <polinasok@users.noreply.github.com>
This commit is contained in:
polinasok 2021-02-21 11:17:14 -08:00 committed by GitHub
parent 780bcac9cc
commit 54d0d56b9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

@ -170,13 +170,11 @@ func testOutput(t *testing.T, dlvbin, output string, delveCmds []string) (stdout
_, err = os.Stat(debugbin) _, err = os.Stat(debugbin)
if err == nil { if err == nil {
if strings.ToLower(os.Getenv("TRAVIS")) == "true" && runtime.GOOS == "windows" { // Sometimes delve on Windows can't remove the built binary before
// Sometimes delve on Travis on Windows can't remove the built binary before // exiting and gets an "Access is denied" error when trying.
// exiting and gets an "Access is denied" error when trying. // This used to make this test flaky. See: https://travis-ci.com/go-delve/delve/jobs/296325131)
// Just ignore it. // But we have now added a delay to gobuild.Remove. If this flakes again,
// See: https://travis-ci.com/go-delve/delve/jobs/296325131 // adjust it. Leaving temporary files behind can be annoying to users.
return
}
t.Errorf("running %q: file %v was not deleted\nstdout is %q, stderr is %q", delveCmds, debugbin, stdout, stderr) t.Errorf("running %q: file %v was not deleted\nstdout is %q, stderr is %q", delveCmds, debugbin, stdout, stderr)
return return
} }

@ -6,6 +6,8 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"runtime"
"time"
"github.com/go-delve/delve/pkg/config" "github.com/go-delve/delve/pkg/config"
"github.com/go-delve/delve/pkg/goversion" "github.com/go-delve/delve/pkg/goversion"
@ -15,6 +17,12 @@ import (
// This can be used to remove the temporary binary generated for the session. // This can be used to remove the temporary binary generated for the session.
func Remove(path string) { func Remove(path string) {
err := os.Remove(path) err := os.Remove(path)
// Open files can be removed on Unix, but not on Windows, where there also appears
// to be a delay in releasing the binary when the process exits. So we try again.
if err != nil && runtime.GOOS == "windows" {
time.Sleep(10 * time.Microsecond)
err = os.Remove(path)
}
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "could not remove %v: %v\n", path, err) fmt.Fprintf(os.Stderr, "could not remove %v: %v\n", path, err)
} }