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
cmd/vet: copylock where sync.Mutex gets immediately reassigned on the following line #20261
Comments
You can change the Session type by putting most of its fields (but not m) in a nested struct. See the fix for the same issue in Regexp.Copy: https://go-review.googlesource.com/c/20841/. |
By the way, I wrote this exact same kind of code originally for Regexp.Copy in https://go-review.googlesource.com/c/16110 and the consensus of the discussion in https://go-review.googlesource.com/c/20841 seemed to be that it's not actually safe. (Or at least, the race detector will complain.) So I don't think it's just a vet issue. |
Perhaps the reasoning is: |
IMHO, the copylock check works as intended in this case due to the reason @cespare mentioned above |
I agree, you're copying a lock and that's what it's complaining about. The simplest workaround would be to make m a pointer, and then say scopy := *session although that does allocate more. Alternatively, copy all fields but the mutex in a copy method. |
Here's some code that triggers an error in
go vet
(initially found in go-mgo/mgo#435):Basically, give me a new object that has all of the same fields as the old one, minus these two that I want to give new values to. This triggers a vet error:
You can see it gets overwritten on the next line. Since
scopy
is only live in the function, the code seems like it's correct.We could copy over all of the fields in the struct, but that would be cumbersome as it's pretty large.
Wondering if you had thoughts here.
The text was updated successfully, but these errors were encountered: