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

lua cluster_specifier: fix crash in getCluster() #37073

Merged
merged 1 commit into from
Nov 9, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class ClusterWrapper : public Filters::Common::Lua::BaseLuaObject<ClusterWrapper
};
}

void onMarkDead() override { cluster_.reset(); }

private:
DECLARE_LUA_FUNCTION(ClusterWrapper, luaNumConnections);
DECLARE_LUA_FUNCTION(ClusterWrapper, luaNumRequests);
Expand All @@ -74,7 +76,7 @@ class ClusterWrapper : public Filters::Common::Lua::BaseLuaObject<ClusterWrapper
Upstream::ClusterInfoConstSharedPtr cluster_;
};

using ClusterRef = Filters::Common::Lua::LuaRef<ClusterWrapper>;
using ClusterRef = Filters::Common::Lua::LuaDeathRef<ClusterWrapper>;

class RouteHandleWrapper : public Filters::Common::Lua::BaseLuaObject<RouteHandleWrapper> {
public:
Expand All @@ -91,7 +93,10 @@ class RouteHandleWrapper : public Filters::Common::Lua::BaseLuaObject<RouteHandl
// All embedded references should be reset when the object is marked dead. This is to ensure that
// we won't do the resetting in the destructor, which may be called after the referenced
// coroutine's lua_State is closed. And if that happens, the resetting will cause a crash.
void onMarkDead() override { headers_wrapper_.reset(); }
void onMarkDead() override {
headers_wrapper_.reset();
clusters_.clear();
}

private:
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ TEST_F(LuaClusterSpecifierPluginTest, ClusterMethods) {
Http::TestRequestHeaderMapImpl headers{{":path", "/"}};
auto route = plugin_->route(mock_route, headers);
EXPECT_EQ("pass", route->routeEntry()->clusterName());

// Force the runtime to gc and destroy all the userdata.
config_->perLuaCodeSetup()->runtimeGC();
}

} // namespace Lua
Expand Down