Skip to content

Commit b70cdfa

Browse files
committed
race issue fix
1 parent 55b3812 commit b70cdfa

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

xhttp/serve.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,25 @@ func Serve(ctx context.Context, shutdownTimeout time.Duration, s *http.Server, l
2727
return ctx
2828
}
2929

30-
done := make(chan error, 1)
30+
serverClosed := make(chan struct{})
31+
var serverError error
3132
go func() {
32-
done <- s.Serve(l)
33+
serverError = s.Serve(l)
34+
close(serverClosed)
3335
}()
3436

3537
select {
36-
case err := <-done:
37-
return err
38+
case <-serverClosed:
39+
return serverError
3840
case <-ctx.Done():
39-
ctx = xcontext.WithoutCancel(ctx)
40-
ctx, cancel := context.WithTimeout(ctx, shutdownTimeout)
41+
shutdownCtx, cancel := context.WithTimeout(xcontext.WithoutCancel(ctx), shutdownTimeout)
4142
defer cancel()
42-
return s.Shutdown(ctx)
43+
44+
err := s.Shutdown(shutdownCtx)
45+
<-serverClosed // Wait for server to exit
46+
if err != nil {
47+
return err
48+
}
49+
return serverError
4350
}
4451
}

0 commit comments

Comments
 (0)