Skip to content

Commit 5a3985e

Browse files
authored
test: Use global swagger handler to avoid data race in tests (#5668)
1 parent 41cefef commit 5a3985e

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

coderd/coderd.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ import (
6060
"github.com/coder/coder/tailnet"
6161
)
6262

63+
// We must only ever instantiate one httpSwagger.Handler because of a data race
64+
// inside the handler. This issue is triggered by tests that create multiple
65+
// coderd instances.
66+
//
67+
// See https://github.com/swaggo/http-swagger/issues/78
68+
var globalHTTPSwaggerHandler http.HandlerFunc
69+
70+
func init() {
71+
globalHTTPSwaggerHandler = httpSwagger.Handler(httpSwagger.URL("/swagger/doc.json"))
72+
}
73+
6374
// Options are requires parameters for Coder to start.
6475
type Options struct {
6576
AccessURL *url.URL
@@ -599,7 +610,9 @@ func New(options *Options) *API {
599610
// Swagger UI requires the URL trailing slash. Otherwise, the browser tries to load /assets
600611
// from http://localhost:8080/assets instead of http://localhost:8080/swagger/assets.
601612
r.Get("/swagger", http.RedirectHandler("/swagger/", http.StatusTemporaryRedirect).ServeHTTP)
602-
r.Get("/swagger/*", httpSwagger.Handler(httpSwagger.URL("/swagger/doc.json")))
613+
// See globalHTTPSwaggerHandler comment as to why we use a package
614+
// global variable here.
615+
r.Get("/swagger/*", globalHTTPSwaggerHandler)
603616
}
604617

605618
r.NotFound(compressHandler(http.HandlerFunc(api.siteHandler.ServeHTTP)).ServeHTTP)

0 commit comments

Comments
 (0)