-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
Comments
package main
import "time"
func main() {
illegalTimerCopy := *time.NewTimer(time.Second)
illegalTimerCopy.Stop() // block for ever
} |
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 |
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
That doc was added for #8776. So I think this has always been documented as not working. |
If we do decide to add it to vet, copylock is a natural home for implementing this (if a mildly inappropriate name). |
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
doesn't mention that after creation you cannot copy the timer's value. |
Unless we get a lot of reports about breakage due to this, we aren't going to change it. |
We did run into this issue after upgrading our codebase to go 1.23.3. But using The resulting panic was very misleading:
The timer was created as in the issue description. From my point of view |
@ianlancetaylor I think the golang tools should warn users of this type of misuse. This behavior is not documented, and the resulting panic message is very confusing – especially for those new to the language. Adding |
Just in the interests of connecting these two issues, note that #70811 is currently proposing a more general solution to warning about unwise copying. If that proposal were accepted then perhaps this proposal would become a library change to add a NoCopy sigil field to the type rather than to directly change (I'm not intending this comment as favor for one design over the other; I just want to make sure there's cross-links between these two slightly-overlapping proposals.) |
@l0nax To be clear, I'm fine with a vet check. When I say that we aren't going to change the behavior, I mean that we aren't going to restore the 1.22 behavior, not that we aren't going to add a vet check. |
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 afatal error: ts set in timer
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.The text was updated successfully, but these errors were encountered: