Skip to content

Commit 90c49bc

Browse files
joelanfordgrokspawn
authored andcommitted
do not fatally exit from serve commands (#1016)
When we fatally exit directly in the serve functions, we do not give a chance for the registered cleanup functions to run, resulting in temporary files being left behind. This commit ensures that we always cleanly return an error from the serve function, thus ensuring that defered cleanups always happen. Signed-off-by: Joe Lanford <[email protected]> Upstream-repository: operator-registry Upstream-commit: 215f413e76e3fb544b0d450acdbab41c756dbf59
1 parent f8c466a commit 90c49bc

File tree

4 files changed

+64
-30
lines changed

4 files changed

+64
-30
lines changed

staging/operator-registry/cmd/opm/registry/serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func serveFunc(cmd *cobra.Command, _ []string) error {
115115

116116
lis, err := net.Listen("tcp", ":"+port)
117117
if err != nil {
118-
logger.Fatalf("failed to listen: %s", err)
118+
return fmt.Errorf("failed to listen: %s", err)
119119
}
120120

121121
timeout, err := cmd.Flags().GetString("timeout-seconds")

staging/operator-registry/cmd/opm/serve/serve.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ will not be reflected in the served content.
8080

8181
func (s *serve) run(ctx context.Context) error {
8282
p := newProfilerInterface(s.pprofAddr, s.logger)
83-
p.startEndpoint()
83+
if err := p.startEndpoint(); err != nil {
84+
return fmt.Errorf("could not start pprof endpoint: %v", err)
85+
}
8486
if err := p.startCpuProfileCache(); err != nil {
8587
return fmt.Errorf("could not start CPU profile: %v", err)
8688
}
@@ -115,7 +117,7 @@ func (s *serve) run(ctx context.Context) error {
115117

116118
lis, err := net.Listen("tcp", ":"+s.port)
117119
if err != nil {
118-
s.logger.Fatalf("failed to listen: %s", err)
120+
return fmt.Errorf("failed to listen: %s", err)
119121
}
120122

121123
grpcServer := grpc.NewServer()
@@ -129,7 +131,9 @@ func (s *serve) run(ctx context.Context) error {
129131
return grpcServer.Serve(lis)
130132
}, func() {
131133
grpcServer.GracefulStop()
132-
p.stopEndpoint(p.logger.Context)
134+
if err := p.stopEndpoint(ctx); err != nil {
135+
s.logger.Warnf("error shutting down pprof server: %v", err)
136+
}
133137
})
134138

135139
}
@@ -147,7 +151,8 @@ type profilerInterface struct {
147151
cacheReady bool
148152
cacheLock sync.RWMutex
149153

150-
logger *logrus.Entry
154+
logger *logrus.Entry
155+
closeErr chan error
151156
}
152157

153158
func newProfilerInterface(a string, log *logrus.Entry) *profilerInterface {
@@ -162,10 +167,10 @@ func (p *profilerInterface) isEnabled() bool {
162167
return p.addr != ""
163168
}
164169

165-
func (p *profilerInterface) startEndpoint() {
170+
func (p *profilerInterface) startEndpoint() error {
166171
// short-circuit if not enabled
167172
if !p.isEnabled() {
168-
return
173+
return nil
169174
}
170175

171176
mux := http.NewServeMux()
@@ -181,14 +186,22 @@ func (p *profilerInterface) startEndpoint() {
181186
Handler: mux,
182187
}
183188

184-
// goroutine exits with main
185-
go func() {
189+
lis, err := net.Listen("tcp", p.addr)
190+
if err != nil {
191+
return err
192+
}
186193

187-
p.logger.Info("starting pprof endpoint")
188-
if err := p.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
189-
p.logger.Fatal(err)
190-
}
194+
p.closeErr = make(chan error)
195+
go func() {
196+
p.closeErr <- func() error {
197+
p.logger.Info("starting pprof endpoint")
198+
if err := p.server.Serve(lis); err != nil && !errors.Is(err, http.ErrServerClosed) {
199+
return err
200+
}
201+
return nil
202+
}()
191203
}()
204+
return nil
192205
}
193206

194207
func (p *profilerInterface) startCpuProfileCache() error {
@@ -222,10 +235,14 @@ func (p *profilerInterface) httpHandler(w http.ResponseWriter, r *http.Request)
222235
w.Write(p.cache.Bytes())
223236
}
224237

225-
func (p *profilerInterface) stopEndpoint(ctx context.Context) {
238+
func (p *profilerInterface) stopEndpoint(ctx context.Context) error {
239+
if !p.isEnabled() {
240+
return nil
241+
}
226242
if err := p.server.Shutdown(ctx); err != nil {
227-
p.logger.Fatal(err)
243+
return err
228244
}
245+
return <-p.closeErr
229246
}
230247

231248
func (p *profilerInterface) isCacheReady() bool {

vendor/github.com/operator-framework/operator-registry/cmd/opm/registry/serve.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-registry/cmd/opm/serve/serve.go

Lines changed: 31 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)