Skip to content

Commit b93a8fe

Browse files
committed
Ensure closure on error
1 parent a72d04c commit b93a8fe

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

enterprise/coderd/coderd.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
// New constructs an Enterprise coderd API instance.
3636
// This handler is designed to wrap the AGPL Coder code and
3737
// layer Enterprise functionality on top as much as possible.
38-
func New(ctx context.Context, options *Options) (*API, error) {
38+
func New(ctx context.Context, options *Options) (api *API, err error) {
3939
if options.EntitlementsUpdateInterval == 0 {
4040
options.EntitlementsUpdateInterval = 10 * time.Minute
4141
}
@@ -52,13 +52,18 @@ func New(ctx context.Context, options *Options) (*API, error) {
5252
options.Options.Authorizer = rbac.NewCachingAuthorizer(options.PrometheusRegistry)
5353
}
5454
ctx, cancelFunc := context.WithCancel(ctx)
55-
api := &API{
55+
api = &API{
5656
ctx: ctx,
5757
cancel: cancelFunc,
5858

5959
AGPL: coderd.New(options.Options),
6060
Options: options,
6161
}
62+
defer func() {
63+
if err != nil {
64+
_ = api.Close()
65+
}
66+
}()
6267

6368
api.AGPL.Options.SetUserGroups = api.setUserGroups
6469

@@ -312,8 +317,12 @@ type API struct {
312317

313318
func (api *API) Close() error {
314319
api.cancel()
315-
_ = api.replicaManager.Close()
316-
_ = api.derpMesh.Close()
320+
if api.replicaManager != nil {
321+
_ = api.replicaManager.Close()
322+
}
323+
if api.derpMesh != nil {
324+
_ = api.derpMesh.Close()
325+
}
317326
return api.AGPL.Close()
318327
}
319328

0 commit comments

Comments
 (0)