diff --git a/lib/orch_zmq_config.cpp b/lib/orch_zmq_config.cpp index 44135a18..09bc66e0 100644 --- a/lib/orch_zmq_config.cpp +++ b/lib/orch_zmq_config.cpp @@ -72,7 +72,10 @@ std::shared_ptr swss::create_zmq_server(std::string zmq_address } SWSS_LOG_NOTICE("Create ZMQ server with address: %s, vrf: %s", zmq_address.c_str(), vrf.c_str()); - return std::make_shared(zmq_address, vrf); + + // To prevent message loss between ZmqServer's bind operation and the creation of ZmqProducerStateTable, + // use lazy binding and call bind() only after the handler has been registered. + return std::make_shared(zmq_address, vrf, true); } bool swss::get_feature_status(std::string feature, bool default_value) diff --git a/orchagent/main.cpp b/orchagent/main.cpp index 474ce1b1..ba67b863 100644 --- a/orchagent/main.cpp +++ b/orchagent/main.cpp @@ -854,6 +854,14 @@ int main(int argc, char **argv) syncd_apply_view(); } + if (zmq_server) + { + // To prevent message loss between ZmqServer's bind operation and the creation of ZmqProducerStateTable, + // use lazy binding and call bind() only after the handler has been registered. + zmq_server->bind(); + SWSS_LOG_NOTICE("ZMQ channel on the northbound side of Orchagent successfully bound: %s, %s", zmq_server_address.c_str(), vrf.c_str()); + } + orchDaemon->start(heartBeatInterval); return 0; diff --git a/tests/mock_tests/zmq_orch_ut.cpp b/tests/mock_tests/zmq_orch_ut.cpp index 8878975f..21a02352 100644 --- a/tests/mock_tests/zmq_orch_ut.cpp +++ b/tests/mock_tests/zmq_orch_ut.cpp @@ -79,6 +79,7 @@ TEST(ZmqOrchTest, CreateZmqClient) auto zmq_client = swss::create_zmq_client(zmq_server_address); zmq_server->registerMessageHandler("test_db", "test_table", &zmq_handler); + zmq_server->bind(); std::vector value; zmq_client->sendMsg("test_db", "test_table", value); @@ -127,4 +128,4 @@ TEST(ZmqOrchTest, GetFeatureStatusException) config_db.hset("DEVICE_METADATA|localhost", HGET_THROW_EXCEPTION_FIELD_NAME, "true"); enabled = swss::get_feature_status(HGET_THROW_EXCEPTION_FIELD_NAME, false); EXPECT_FALSE(enabled); -} \ No newline at end of file +}