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

Fix loop variable captured by func literal #2660

Conversation

peczenyj
Copy link
Contributor

@peczenyj peczenyj commented Oct 5, 2023

Description

by running go vet ./... with go latest version there are some warning about loop variable captured by func literal

this is linked to

https:/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable

and

https:/golang/go/wiki/LoopvarExperiment

$ go version
go version go1.21.1 linux/amd64

$ go vet ./...
# github.com/gofiber/fiber/v2/internal/go-ole
internal/go-ole/variant.go:15:21: possible misuse of unsafe.Pointer
internal/go-ole/variant.go:23:22: possible misuse of unsafe.Pointer
internal/go-ole/variant.go:33:42: possible misuse of unsafe.Pointer
# github.com/gofiber/fiber/v2/middleware/compress
middleware/compress/compress_test.go:60:30: loop variable level captured by func literal
# github.com/gofiber/fiber/v2/middleware/filesystem
middleware/filesystem/filesystem_test.go:124:63: loop variable tt captured by func literal
middleware/filesystem/filesystem_test.go:126:25: loop variable tt captured by func literal
middleware/filesystem/filesystem_test.go:128:7: loop variable tt captured by func literal
middleware/filesystem/filesystem_test.go:130:26: loop variable tt captured by func literal
# github.com/gofiber/fiber/v2/middleware/pprof
middleware/pprof/pprof_test.go:109:32: loop variable sub captured by func literal
middleware/pprof/pprof_test.go:110:7: loop variable sub captured by func literal
middleware/pprof/pprof_test.go:138:48: loop variable sub captured by func literal
middleware/pprof/pprof_test.go:139:7: loop variable sub captured by func literal

I have no idea how to fix the possible misuse of unsafe.Pointer (this can be a false positive, someone with experience in windows and go unsafe must check) but the loop variable is easier

However this trigger some random 500 errors in the middleware/pprof because some handlers are not supposed to be called more than once at same time.

We can see outputs like

Could not enable tracing: runtime error: tracing is already enabled

or

Could not enable CPU profiling: cpu profiling already in use

I remove the t.Parallel() of such tests

Type of change

minor change in tests

Checklist:

  • [ x] For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • [ x] I have performed a self-review of my own code
  • [ x] I have commented my code, particularly in hard-to-understand areas
  • [ x] I have made corresponding changes to the documentation - /docs/ directory for https://docs.gofiber.io/
  • [ x] I have added tests that prove my fix is effective or that my feature works
  • [ x] New and existing unit tests pass locally with my changes
  • [ x] If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • [ x] I tried to make my code as fast as possible with as few allocations as possible
  • [ x] For new code I have written benchmarks so that they can be analyzed and improved

Commit formatting:

Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/

@ReneWerner87 ReneWerner87 merged commit 0d33546 into gofiber:master Oct 5, 2023
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants