Skip to content

Commit 401ba7b

Browse files
committed
Ensure closure on error
1 parent a72d04c commit 401ba7b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

enterprise/coderd/coderd.go

+12-3
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, err error) {
3939
if options.EntitlementsUpdateInterval == 0 {
4040
options.EntitlementsUpdateInterval = 10 * time.Minute
4141
}
@@ -59,6 +59,11 @@ func New(ctx context.Context, options *Options) (*API, error) {
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)