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

proposal: cmd/vet: warn about copying a time.Timer value #69186

Open
tulir opened this issue Aug 31, 2024 · 6 comments
Open

proposal: cmd/vet: warn about copying a time.Timer value #69186

tulir opened this issue Aug 31, 2024 · 6 comments
Labels
Milestone

Comments

@tulir
Copy link

tulir commented Aug 31, 2024

The following code works fine on Go 1.22, but breaks on Go 1.23.0 on the http.DefaultClient.Do() line. On Linux, it freezes the entire runtime and never continues. On macOS, it throws a fatal error: ts set in timer

package main

import (
	"fmt"
	"net/http"
	"time"
)

func main() {
	illegalTimerCopy := *time.NewTimer(10 * time.Second)
	go func() {
		illegalTimerCopy.Reset(10 * time.Second)
	}()
	req, err := http.NewRequest(http.MethodGet, "https://example.com", nil)
	fmt.Println("Request created", err)
	_, err = http.DefaultClient.Do(req)
	fmt.Println("Request completed", err)
}

It's presumably somehow caused by the Go 1.23 timer changes, but I'm not sure how exactly, so I don't know if it's a bug or a feature. Assuming it's not a bug, it would be nice if go vet could report that similar to the mutex copy warnings.

@zigo101
Copy link

zigo101 commented Aug 31, 2024

package main

import "time"

func main() {
	illegalTimerCopy := *time.NewTimer(time.Second)
	illegalTimerCopy.Stop() // block for ever
}

@ianlancetaylor
Copy link
Contributor

I'm kind of surprised this ever worked. I don't think it would work in general cases. It might work in cases where you are careful to call Reset after the copy before doing anything else.

@ianlancetaylor ianlancetaylor changed the title time: copying a time.Timer breaks on Go 1.23 cmd/vet: warn about copying a time.Timer value Aug 31, 2024
@ianlancetaylor ianlancetaylor changed the title cmd/vet: warn about copying a time.Timer value proposal: cmd/vet: warn about copying a time.Timer value Aug 31, 2024
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Aug 31, 2024
@gopherbot gopherbot added this to the Proposal milestone Aug 31, 2024
@ianlancetaylor
Copy link
Contributor

Unless we get a lot of reports about breakage due to this, we aren't going to change it. So changing this to a proposal for a vet check. Though I don't know how useful a vet check would be--I don't know how often this occurs. At least since Go 1.4 the time.Timer docs have said

// A Timer must be created with NewTimer or AfterFunc.

That doc was added for #8776. So I think this has always been documented as not working.

@timothy-king
Copy link
Contributor

If we do decide to add it to vet, copylock is a natural home for implementing this (if a mildly inappropriate name).

@gnvk
Copy link

gnvk commented Sep 9, 2024

Just my 2 cents on this: in a real code base this can lead to a crash that doesn't trivially show the root cause. And the exact same code did work in go 1.22, so in my opinion something should be done about this, because technically this is a breaking change. The linked documentation

A Timer must be created with NewTimer or AfterFunc

doesn't mention that after creation you cannot copy the timer's value.

@ianlancetaylor
Copy link
Contributor

Unless we get a lot of reports about breakage due to this, we aren't going to change it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

6 participants