Skip to content

Commit d5bfca8

Browse files
committed
fix memory leak
- closed the request body in admission webhook - closed filters after validation - added a check in the lifo filter to ensure the queue is not nil during closing Signed-off-by: Veronika Volokitina <[email protected]>
1 parent 19bd6bb commit d5bfca8

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

dataclients/kubernetes/admission/admission.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func Handler(admitter admitter) http.HandlerFunc {
7171
return
7272
}
7373

74+
defer r.Body.Close()
75+
7476
body, err := io.ReadAll(r.Body)
7577
if err != nil {
7678
log.Errorf("Failed to read request: %v", err)

filters/scheduler/lifo.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ func (l *lifoFilter) GetQueue() *scheduler.Queue {
235235

236236
// Close will cleanup underlying queues
237237
func (l *lifoFilter) Close() error {
238-
l.queue.Close()
238+
if l.queue != nil {
239+
l.queue.Close()
240+
}
239241
return nil
240242
}
241243

@@ -286,7 +288,9 @@ func (l *lifoGroupFilter) GetQueue() *scheduler.Queue {
286288

287289
// Close will cleanup underlying queues
288290
func (l *lifoGroupFilter) Close() error {
289-
l.queue.Close()
291+
if l.queue != nil {
292+
l.queue.Close()
293+
}
290294
return nil
291295
}
292296

routing/datasource.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,13 @@ func processTreePredicates(r *Route, predicateList []*eskip.Predicate) error {
475475
// ValidateRoute processes a route definition for the routing table.
476476
// This function is exported to be used by validation webhooks.
477477
func ValidateRoute(o *Options, def *eskip.Route) (*Route, error) {
478-
return processRouteDef(o, mapPredicates(o.Predicates), def)
478+
route, err := processRouteDef(o, mapPredicates(o.Predicates), def)
479+
if err != nil {
480+
return nil, err
481+
}
482+
483+
defer closeFilterClosers(route.Filters)
484+
return route, nil
479485
}
480486

481487
// processes a route definition for the routing table
@@ -489,6 +495,11 @@ func processRouteDef(o *Options, cpm map[string]PredicateSpec, def *eskip.Route)
489495
if err != nil {
490496
return nil, err
491497
}
498+
defer func() {
499+
if err != nil {
500+
closeFilterClosers(fs)
501+
}
502+
}()
492503

493504
def, err = mergeLegacyNonTreePredicates(def)
494505
if err != nil {
@@ -563,15 +574,19 @@ type routeTable struct {
563574
func (rt *routeTable) close() {
564575
rt.once.Do(func() {
565576
for _, route := range rt.routes {
566-
for _, f := range route.Filters {
567-
if fc, ok := f.Filter.(filters.FilterCloser); ok {
568-
fc.Close()
569-
}
570-
}
577+
closeFilterClosers(route.Filters)
571578
}
572579
})
573580
}
574581

582+
func closeFilterClosers(rfs []*RouteFilter) {
583+
for _, f := range rfs {
584+
if fc, ok := f.Filter.(filters.FilterCloser); ok {
585+
_ = fc.Close()
586+
}
587+
}
588+
}
589+
575590
// receives the next version of the routing table on the output channel,
576591
// when an update is received on one of the data clients.
577592
func receiveRouteMatcher(o Options, out chan<- *routeTable, quit <-chan struct{}) {

0 commit comments

Comments
 (0)