Fix redis client and C string memory leaks in events client - #774
Open
saravanan-i-CLS wants to merge 1 commit into
Open
saravanan-i-CLS wants to merge 1 commit into
saravanan-i-CLS wants to merge 1 commit into
Conversation
update_stats() created a COUNTERS_DB redis client per events subscription but never closed it, leaking a TCP socket and its background goroutines on every subscribe/unsubscribe cycle. Add a deferred Close(). Set_heartbeat() passed the result of C.CString() inline to the C call and never freed it. C.CString always allocates and the callee (event_set_global_options, a const char *) does not take ownership, so the buffer leaked on every call. Capture the pointer and defer C.free(). Add TestUpdateStatsClosesRedisClient to verify the redis client is closed on subscription teardown. Signed-off-by: Saravanan <saravan@celestica.com>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
/azp run |
saravanan-i-CLS
marked this pull request as ready for review
August 31, 2026 06:16
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Hi, there are workflow run(s) waiting for approval, you may be first-time contributor. I will notify maintainers to help approve once PR is approved. Thanks! ---Powered by SONiC BuildBot
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why I did it
The gNMI events client had two resource leaks in sonic_data_client/events_client.go, both reachable in normal operation:
COUNTERS_DB redis client leak. update_stats() creates a redis.Client for COUNTERS_DB once per events subscription and never closes it. Each subscribe/unsubscribe cycle abandoned a client, leaking its TCP socket and internal background goroutines. Over time (repeated telemetry event subscriptions) this accumulates.
C.CString leak. Set_heartbeat() passed C.CString(s) inline to event_set_global_options() without ever freeing it. C.CString always allocates on the C heap and the caller owns the buffer; the callee takes a const char * and does not free it, so every call leaked the string (invoked on each events subscribe that carries a heartbeat path parameter).
How I did it
How to verify it
cd src/sonic-gnmi
go test ./sonic_data_client/ -run TestUpdateStatsClosesRedisClient -v
The test fails without the defer rclient.Close() fix (closeCount == 0) and passes with it. The C.CString fix is verifiable under the LeakSanitizer harness (make check_memleak); note sonic_data_client is currently excluded from that target in the Makefile due to unrelated libyang leaks in the test environment, so the CString fix is covered by code inspection against the existing free pattern.
Which release branch to backport (provide reason below if selected)
Not Applicable
Tested branch
Test result
master:
$ cd src/sonic-gnmi
$ go test ./sonic_data_client/ -run TestUpdateStatsClosesRedisClient -v
=== RUN TestUpdateStatsClosesRedisClient
--- PASS: TestUpdateStatsClosesRedisClient (~1.0s)
PASS
ok github.com/sonic-net/sonic-gnmi/sonic_data_client
Description for the changelog
[sonic-gnmi] Fix redis client and C string memory leaks in the gNMI events client
Link to config_db schema for YANG module changes
N/A — no CONFIG_DB tables or YANG models are added or changed. This is a code-only resource-cleanup fix in the events data client.
A picture of a cute animal (not mandatory but encouraged)