Skip to content

Commit d1378d1

Browse files
authored
fix: Clean up interfaceLockMap entries on endpoint deletion (#1249)
# Description The packetParser was creating entries in interfaceLockMap for each new interface but failing to remove them when interfaces were deleted. In environments with high pod counts and frequent churn, this caused a memory leak as the map grew indefinitely. ## Related Issue [Potential memory leak in packetparser's interfaceLockMap #1236](#1236) ## Checklist - [X] I have read the [contributing documentation](https://retina.sh/docs/contributing). - [X] I signed and signed-off the commits (`git commit -S -s ...`). See [this documentation](https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification) on signing commits. - [X] I have correctly attributed the author(s) of the code. - [X] I have tested the changes locally. - [X] I have followed the project's style guidelines. - [ ] I have updated the documentation, if necessary. - [X] I have added tests, if applicable. ## Screenshots (if applicable) or Testing Completed Please add any relevant screenshots or GIFs to showcase the changes made. ## Additional Notes ### Solution - Added cleanup of interfaceLockMap entries in the EndpointDeleted case - Improved mutex handling logic to prevent resource leaks - Updated test cases to verify proper cleanup of both tcMap and interfaceLockMap ### Testing - Added comprehensive test coverage for interface deletion scenario - Verified cleanup of both maps in test cases - Tested with high pod churn scenarios ### Impact This fix prevents memory leaks in environments with frequent pod creation/deletion, improving the overall stability and resource usage of the system. --- Please refer to the [CONTRIBUTING.md](../CONTRIBUTING.md) file for more information on how to contribute to this project. --------- Signed-off-by: Yerlan Baiturinov <[email protected]>
1 parent 378b0b4 commit d1378d1

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

pkg/plugin/packetparser/packetparser_linux.go

+2
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,8 @@ func (p *packetParser) endpointWatcherCallbackFn(obj interface{}) {
400400
// Delete from map.
401401
p.tcMap.Delete(ifaceKey)
402402
}
403+
// Delete from lock map.
404+
p.interfaceLockMap.Delete(ifaceKey)
403405
default:
404406
// Unknown.
405407
p.l.Debug("Unknown event", zap.String("type", event.Type.String()))

pkg/plugin/packetparser/packetparser_linux_test.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,24 @@ func TestEndpointWatcherCallbackFn_EndpointDeleted(t *testing.T) {
162162
ctrl := gomock.NewController(t)
163163
defer ctrl.Finish()
164164

165+
// Initialize packetParser with both maps.
165166
p := &packetParser{
166167
cfg: cfgPodLevelEnabled,
167168
l: log.Logger().Named("test"),
168169
interfaceLockMap: &sync.Map{},
170+
tcMap: &sync.Map{},
169171
}
170-
p.tcMap = &sync.Map{}
172+
173+
// Create test interface attributes.
171174
linkAttr := netlink.LinkAttrs{
172175
Name: "test",
173176
HardwareAddr: []byte("test"),
174177
NetNsID: 1,
175178
}
176179
key := ifaceToKey(linkAttr)
180+
181+
// Pre-populate both maps to simulate existing interface
182+
p.interfaceLockMap.Store(key, &sync.Mutex{})
177183
p.tcMap.Store(key, &tcValue{nil, &tc.Object{}})
178184

179185
// Create EndpointDeleted event.
@@ -182,10 +188,16 @@ func TestEndpointWatcherCallbackFn_EndpointDeleted(t *testing.T) {
182188
Obj: linkAttr,
183189
}
184190

191+
// Execute the callback.
185192
p.endpointWatcherCallbackFn(e)
186193

187-
_, ok := p.tcMap.Load(key)
188-
assert.False(t, ok)
194+
// Verify both maps are cleaned up.
195+
_, tcMapExists := p.tcMap.Load(key)
196+
_, lockMapExists := p.interfaceLockMap.Load(key)
197+
198+
// Assert both maps are cleaned up
199+
assert.False(t, tcMapExists, "tcMap entry should be deleted")
200+
assert.False(t, lockMapExists, "interfaceLockMap entry should be deleted")
189201
}
190202

191203
func TestCreateQdiscAndAttach(t *testing.T) {

0 commit comments

Comments
 (0)