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

Data race in Handler, modifies global from swaggo/files package but sync.Once protection is function scoped #78

Open
mafredri opened this issue Jan 11, 2023 · 2 comments

Comments

@mafredri
Copy link

As per title, the Handler has a data race in assigning Prefix due to the sync.Once being function scoped and Prefix existing in the swaggo/files package.

The problematic code:

http-swagger/swagger.go

Lines 157 to 159 in c8d62bf

once.Do(func() {
handler.Prefix = matches[1]
})

This means that any reads to files.Handler.Prefix are globally unsafe across all swaggo packages.

The race can be avoided by only ever invoking one handler. This is not very likely to happen in practice, but problematic in tests. The way it's written also unfortunately means it's impossible to serve on two different endpoints (just an observed behavior, not a requirement).

@OrkhanAlikhanov
Copy link

This happens in practice as well. I have 2 routes defined for 2 different swaggers files. /swagger/*any and /swagger-swe/*any and I pass different handlers for them httpSwagger.Handler() and httpSwagger.Handler(httpSwagger.InstanceName("swe")). However only one of them works.

@OrkhanAlikhanov
Copy link

The same bug in gin-swagger package too: swaggo/gin-swagger#225 swaggo/gin-swagger#234

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

No branches or pull requests

2 participants