Skip to content

Commit

Permalink
fix: race condition in setup
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Nov 3, 2022
1 parent cf1eb9d commit 013b5fa
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions internal/driver/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,16 @@ func (r *RegistryDefault) ServeAll(ctx context.Context) error {

eg := &errgroup.Group{}

eg.Go(r.serveRead(innerCtx, doneShutdown))
eg.Go(r.serveWrite(innerCtx, doneShutdown))
eg.Go(r.serveOPLSyntax(innerCtx, doneShutdown))
eg.Go(r.serveMetrics(innerCtx, doneShutdown))
// We need to separate the setup (invoking the functions that return the serve functions) from running the serve
// functions to mitigate race contitions in the HTTP router.
for _, serve := range []func() error{
r.serveRead(innerCtx, doneShutdown),
r.serveWrite(innerCtx, doneShutdown),
r.serveOPLSyntax(innerCtx, doneShutdown),
r.serveMetrics(innerCtx, doneShutdown),
} {
eg.Go(serve)
}

return eg.Wait()
}
Expand Down Expand Up @@ -232,7 +238,7 @@ func multiplexPort(ctx context.Context, log *logrusx.Logger, addr string, router
})

eg.Go(func() error {
if err := restS.Serve(httpL); !errors.Is(err, http.ErrServerClosed) && !errors.Is(err, cmux.ErrServerClosed) {
if err := restS.Serve(httpL); !errors.Is(err, http.ErrServerClosed) && !errors.Is(err, cmux.ErrServerClosed) { // <<- Lookup
return errors.WithStack(err)
}
return nil
Expand Down Expand Up @@ -513,7 +519,7 @@ func (r *RegistryDefault) metricsRouter(ctx context.Context) http.Handler {
router := httprouter.New()

r.PrometheusManager().RegisterRouter(router)
r.MetricsHandler().SetRoutes(router)
r.MetricsHandler().SetRoutes(router) // <<- Register (races with lookup)
n.UseHandler(router)
n.Use(r.PrometheusManager())

Expand Down

0 comments on commit 013b5fa

Please sign in to comment.