Copilot commented on code in PR #13045:
URL: https://github.com/apache/trafficserver/pull/13045#discussion_r3018612755


##########
plugins/lua/ts_lua.cc:
##########
@@ -827,6 +827,34 @@ globalHookHandler(TSCont contp, TSEvent event ATS_UNUSED, 
void *edata)
   return 0;
 }
 
+static int
+shutdownHookHandler(TSCont contp, TSEvent /* event ATS_UNUSED */, void * /* 
edata ATS_UNUSED */)
+{
+  ts_lua_instance_conf *const conf = (ts_lua_instance_conf 
*)TSContDataGet(contp);
+
+  for (int index = 0; index < conf->states; ++index) {
+    ts_lua_main_ctx *const main_ctx = &ts_lua_g_main_ctx_array[index];
+
+    TSMutexLock(main_ctx->mutexp);
+
+    lua_State *const l = main_ctx->lua;
+
+    lua_getglobal(l, TS_LUA_FUNCTION_G_SHUT_DOWN);
+    if (lua_type(l, -1) == LUA_TFUNCTION) {
+      if (lua_pcall(l, 0, 0, 0) != 0) {
+        TSError("[ts_lua][%s] lua_pcall failed: %s", __FUNCTION__, 
lua_tostring(l, -1));
+        lua_pop(l, 1);
+      }
+    } else {
+      lua_pop(l, 1);
+    }

Review Comment:
   `shutdownHookHandler` calls `lua_getglobal()` on `main_ctx->lua`, but 
`ts_lua_add_module()` stores the script globals in `LUA_REGISTRYINDEX[conf]` 
and then replaces `LUA_GLOBALSINDEX` with an empty table. As a result, 
`do_global_shutdown` will not be found/invoked (the hook becomes a no-op). Use 
a coroutine Lua state (like `ts_lua_create_http_ctx(...)->cinfo.routine.lua`) 
or temporarily replace `LUA_GLOBALSINDEX` with `LUA_REGISTRYINDEX[conf]` (as 
done in `ts_lua_del_module()`/`ts_lua_reload_module()`) before calling the 
function.



##########
tests/gold_tests/pluginTest/lua/lua_global_shutdown.test.py:
##########
@@ -0,0 +1,65 @@
+'''
+Test do_global_shut_down lua global plugin hook.
+'''

Review Comment:
   The test description refers to `do_global_shut_down`, but the hook/function 
added by the plugin and documented elsewhere is `do_global_shutdown` (no extra 
underscore). Update the docstring/Summary text to match the actual hook name to 
avoid confusion when grepping failures.



##########
tests/gold_tests/pluginTest/lua/lua_global_shutdown.test.py:
##########
@@ -0,0 +1,65 @@
+'''
+Test do_global_shut_down lua global plugin hook.
+'''
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+Test.Summary = '''
+Test do_global_shut_down lua global plugin hook
+'''

Review Comment:
   `Test.Summary` uses `do_global_shut_down`, but the implemented hook name is 
`do_global_shutdown`. Keeping the exact name consistent in the test metadata 
makes failures easier to interpret and search for.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to