Skip to content

Commit 27ec195

Browse files
committed
context must be canceled to avoid memory leak
1 parent b2d37bc commit 27ec195

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

docker/core/core.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ const (
2222
dockerTimeout = 5 * time.Second
2323
)
2424

25-
func toCtx() context.Context {
26-
c, _ := context.WithTimeout(context.Background(), dockerTimeout)
27-
return c
28-
}
29-
3025
// Core is a wrapper for docker client type things
3126
type Core struct {
3227
dc *client.Client
@@ -100,7 +95,9 @@ func (c *Core) getNrFromCache(s string) *types.NetworkResource {
10095

10196
// GetContainers gets a list of docker containers
10297
func (c *Core) GetContainers() ([]types.Container, error) {
103-
return c.dc.ContainerList(toCtx(), types.ContainerListOptions{})
98+
ctx, cancel := context.WithTimeout(context.Background(), dockerTimeout)
99+
defer cancel()
100+
return c.dc.ContainerList(ctx, types.ContainerListOptions{})
104101
}
105102

106103
// GetNetworkResourceByID gets a network resource by ID (checks cache first)
@@ -114,7 +111,9 @@ func (c *Core) GetNetworkResourceByID(id string) (*types.NetworkResource, error)
114111
}
115112

116113
//netid wasn't in cache, fetch from docker inspect
117-
nnr, err := c.dc.NetworkInspect(toCtx(), id)
114+
ctx, cancel := context.WithTimeout(context.Background(), dockerTimeout)
115+
defer cancel()
116+
nnr, err := c.dc.NetworkInspect(ctx, id)
118117
if err != nil {
119118
log.WithError(err).Error("failed to inspect network")
120119
return nil, err
@@ -138,7 +137,9 @@ func (c *Core) GetNetworkResourceByPool(pool string) (*types.NetworkResource, er
138137

139138
flts := filters.NewArgs()
140139
flts.Add("driver", networkDriverName)
141-
nl, err := c.dc.NetworkList(toCtx(), types.NetworkListOptions{Filters: flts})
140+
ctx, cancel := context.WithTimeout(context.Background(), dockerTimeout)
141+
defer cancel()
142+
nl, err := c.dc.NetworkList(ctx, types.NetworkListOptions{Filters: flts})
142143
if err != nil {
143144
log.WithError(err).Error("failed to list networks")
144145
return nil, err

docker/vxrnet/main.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ const (
2626
shutdownTimeout = 10 * time.Second
2727
)
2828

29-
func shutdownContext() context.Context {
30-
c, _ := context.WithTimeout(context.Background(), shutdownTimeout)
31-
return c
32-
}
33-
3429
func main() {
3530
app := cli.NewApp()
3631
app.Name = "docker-" + network.DriverName
@@ -121,12 +116,16 @@ func Run(ctx *cli.Context) {
121116
case <-c:
122117
}
123118

124-
err = nh.Shutdown(shutdownContext())
119+
nhCtx, nhCtxCancel := context.WithTimeout(context.Background(), shutdownTimeout)
120+
defer nhCtxCancel()
121+
err = nh.Shutdown(nhCtx)
125122
if err != nil {
126123
log.WithField("driver", network.DriverName).WithError(err).Error("error shutting down driver")
127124
}
128125

129-
err = ih.Shutdown(shutdownContext())
126+
ihCtx, ihCtxCancel := context.WithTimeout(context.Background(), shutdownTimeout)
127+
defer ihCtxCancel()
128+
err = ih.Shutdown(ihCtx)
130129
if err != nil {
131130
log.WithField("driver", ipam.DriverName).WithError(err).Error("error shutting down driver")
132131
}

0 commit comments

Comments
 (0)