Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unexpected behavior when deferring file removal #42338

Closed
jftuga opened this issue Nov 2, 2020 · 3 comments
Closed

unexpected behavior when deferring file removal #42338

jftuga opened this issue Nov 2, 2020 · 3 comments

Comments

@jftuga
Copy link

jftuga commented Nov 2, 2020

What version of Go are you using (go version)?

go version go1.15.3 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\john\AppData\Local\go-build
set GOENV=C:\Users\john\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\john\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\john\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\john\AppData\Local\Temp\go-build217132790=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main
import (
	"fmt"
	"io/ioutil"
	"os"
)
func main() {
	file, err := ioutil.TempFile(".", "zzz.")
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	defer os.Remove(file.Name())
	fmt.Println(file.Name())
}

What did you expect to see?

I expected to see the temp file removed, but it remains after the program has ended. I tried this same code on a Raspberry Pi with go1.15.3 and also with go1.14.2 linux/arm. The temp file gets removed with those 2 compilers running under Raspbian GNU/Linux 10 (buster)

This problem is occurring on Windows 10.0.17763.1518

What did you see instead?

Under Win 10, the temp file is not removed. Under Linux, it is. I am also using the built-in Microsoft AV.

@jftuga
Copy link
Author

jftuga commented Nov 2, 2020

I am actually seeing this problem even if I do not place defer before os.Remove().

@randall77
Copy link
Contributor

What if you file.Close() before removing? I understand Windows is not fond of removing files when they are open. Not sure if that would apply here or not.
Please check the error return values of both Remove and Close, they may explain what is going on.

@jftuga
Copy link
Author

jftuga commented Nov 2, 2020

If I call file.Close() before os.Remove(), the file is deleted when the program ends.

If I don't close first, with this code:

func main() {
	file, err := ioutil.TempFile(".", "zzz.")
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	fmt.Println(file.Name())
	// file.Close()
	err = os.Remove(file.Name())
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

I will get this result:

zzz.038806879
remove zzz.038806879: The process cannot access the file because it is being used by another process.

I agree with your understanding about Windows not being fond of removing open files. Since this appears to be an OS issue and not an issue with golang, I am going to close this issue.

@jftuga jftuga closed this as completed Nov 2, 2020
@golang golang locked and limited conversation to collaborators Nov 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants