fix crash issue caused by reload configuration file, which occurs when a vhost is added/removed in the new configuration.

This commit is contained in:
haibo.chen 2025-10-16 13:54:41 +08:00
parent 6f526284a3
commit 9bf2851414
2 changed files with 42 additions and 0 deletions

View File

@ -1509,6 +1509,12 @@ srs_error_t SrsConfig::reload_conf(SrsConfig *conf)
SrsConfDirective *old_vhost = old_root->get("vhost", vhost);
SrsConfDirective *new_vhost = root_->get("vhost", vhost);
// Only compare config when both old and new vhost exist.
// @see https://github.com/ossrs/srs/issues/4529
if (!old_vhost || !new_vhost) {
continue;
}
// chunk_size, only one per vhost.
if (!srs_directive_equals(new_vhost->get("chunk_size"), old_vhost->get("chunk_size"))) {
for (it = subscribes_.begin(); it != subscribes_.end(); ++it) {

View File

@ -111,3 +111,39 @@ VOID TEST(ConfigReloadTest, ReloadVhostChunkSize)
EXPECT_EQ(1, handler.count_true());
handler.reset();
}
VOID TEST(ConfigReloadTest, ReloadAddNewVhost)
{
srs_error_t err = srs_success;
MockReloadHandler handler;
MockSrsReloadConfig conf;
conf.subscribe(&handler);
// Start with one vhost
HELPER_EXPECT_SUCCESS(conf.mock_parse(_MIN_OK_CONF "vhost ossrs.net { chunk_size 60000; }"));
// Add a new vhost - should not crash
HELPER_EXPECT_SUCCESS(conf.do_reload(_MIN_OK_CONF "vhost ossrs.net { chunk_size 60000; } vhost new_vhost { chunk_size 65536; }"));
// Handler should not be triggered for new vhost
EXPECT_TRUE(handler.all_false());
handler.reset();
}
VOID TEST(ConfigReloadTest, ReloadRemoveVhost)
{
srs_error_t err = srs_success;
MockReloadHandler handler;
MockSrsReloadConfig conf;
conf.subscribe(&handler);
// Start with two vhosts
HELPER_EXPECT_SUCCESS(conf.mock_parse(_MIN_OK_CONF "vhost ossrs.net { chunk_size 60000; } vhost old_vhost { chunk_size 65536; }"));
// Remove old_vhost - should not crash
HELPER_EXPECT_SUCCESS(conf.do_reload(_MIN_OK_CONF "vhost ossrs.net { chunk_size 60000; }"));
// Handler should not be triggered for removed vhost
EXPECT_TRUE(handler.all_false());
handler.reset();
}