Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cherry-pick] [202205] Fix memory leak issue in ConfigDBConnector. #706

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/configdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void ConfigDBConnector_Native::db_connect(string db_name, bool wait_for_init, bo
if (wait_for_init)
{
auto& client = get_redis_client(m_db_name);
auto pubsub = client.pubsub();
auto pubsub = make_shared<PubSub>(&client);
auto initialized = client.get(INIT_INDICATOR);
if (!initialized || initialized->empty())
{
Expand Down
3 changes: 3 additions & 0 deletions common/dbconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ class DBConnector : public RedisContext
/* Create new context to DB */
DBConnector *newConnector(unsigned int timeout) const;

#ifndef SWIG
__attribute__((deprecated))
#endif
PubSub *pubsub();

int64_t del(const std::string &key);
Expand Down
15 changes: 15 additions & 0 deletions tests/test_redis_ut.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ def test_ConfigDBConnector():
def test_ConfigDBConnectorSeparator():
db = swsscommon.DBConnector("APPL_DB", 0, True)
config_db = ConfigDBConnector()
# set wait for init to True to cover wait_for_init code.
config_db.db_connect("APPL_DB", False, False)
config_db.get_redis_client(config_db.APPL_DB).flushdb()
config_db.set_entry("TEST_PORT", "Ethernet222", {"alias": "etp2x"})
Expand Down Expand Up @@ -677,3 +678,17 @@ def test_DBConnectFailure():
with pytest.raises(RuntimeError):
db = swsscommon.DBConnector(0, nonexisting_host, 0)

def test_ConfigDBWaitInit():
config_db = ConfigDBConnector()
config_db.connect(wait_for_init=False)
client = config_db.get_redis_client(config_db.CONFIG_DB)
suc = client.set(config_db.INIT_INDICATOR, 1)
assert suc

# set wait for init to True to cover wait_for_init code.
config_db = ConfigDBConnector()
config_db.db_connect(config_db.CONFIG_DB, True, False)

config_db.set_entry("TEST_PORT", "Ethernet111", {"alias": "etp1x"})
allconfig = config_db.get_config()
assert allconfig["TEST_PORT"]["Ethernet111"]["alias"] == "etp1x"