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

runtime: os.OpenFile opens files (sometimes) in exclusive mode on windows/amd64 on tip #69902

Closed
aarzilli opened this issue Oct 16, 2024 · 5 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. OS-Windows

Comments

@aarzilli
Copy link
Contributor

In Go 1.23 (and at least a few versions before it) opening a file twice for writing, was possible (and it continues to be possible on linux):

package main

import "os"

func must(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	fh, err := os.CreateTemp("", "test-*")
	must(err)
	name := fh.Name()
	must(fh.Close())
	fh, err = os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0660)
	must(err)
	fh2, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
	must(err)
	must(fh.Close())
	must(fh2.Close())
}

On tip however this results in an Access is denied error:

panic: open Z:\buildAgent\temp\buildTmp\test-1396926860: Access is denied.
   
   goroutine 1 [running]:
   main.must(...)
     Z:/buildAgent/work/3aca8f960d346a9c/blah.go:7
   main.main()
     Z:/buildAgent/work/3aca8f960d346a9c/blah.go:19 +0x13d
   exit status 2

Furthermore even correcting the order of Open/Close will (sometimes? always?) result in the same Access is denied error, unless a sleep is introduced between the close and the successive open:

package main

import "os"

func must(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	fh, err := os.CreateTemp("", "test-*")
	must(err)
	name := fh.Name()
	must(fh.Close())
	fh, err = os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0660)
	must(err)
	must(fh.Close())
	fh2, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
	must(err)
	must(fh2.Close())
}

It might have something to do with the flags being passed to OpenFile since the problem doesn't happen with the first OpenFile (which is also immediately successive to a Close on the same file). I have not bisected this problem but it was introduced between the 5th and the 10th of October.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Oct 16, 2024
@qmuntal qmuntal self-assigned this Oct 16, 2024
@qmuntal
Copy link
Member

qmuntal commented Oct 16, 2024

Probably related to https://go-review.googlesource.com/c/go/+/618836. Will fix it.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/620575 mentions this issue: syscall: set write access when O_TRUNC is used on Windows

@qmuntal
Copy link
Member

qmuntal commented Oct 18, 2024

@aarzilli could you check if your issue is fixed on tip?

@aarzilli
Copy link
Contributor Author

It does, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. OS-Windows
Projects
None yet
Development

No branches or pull requests

4 participants