Skip to content

Commit

Permalink
lua cluster_specifier: fix crash in getCluster() (envoyproxy#37073)
Browse files Browse the repository at this point in the history
Envoy would crash when the Lua garbage collector ran if `getCluster()`
had been called.

This was added in envoyproxy#36998

Signed-off-by: Greg Greenway <ggreenway@apple.com>
  • Loading branch information
ggreenway authored Nov 9, 2024
1 parent 019f589 commit 95dcf45
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
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

0 comments on commit 95dcf45

Please sign in to comment.