MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / luaMaskCountHook

Function luaMaskCountHook

src/scripting.cpp:1473–1509  ·  view source on GitHub ↗

This is the Lua script "count" hook that we use to detect scripts timeout. */

Source from the content-addressed store, hash-verified

1471
1472/* This is the Lua script "count" hook that we use to detect scripts timeout. */
1473void luaMaskCountHook(lua_State *lua, lua_Debug *ar) {
1474 long long elapsed = elapsedMs(g_pserver->lua_time_start);
1475 UNUSED(ar);
1476 UNUSED(lua);
1477
1478 /* Set the timeout condition if not already set and the maximum
1479 * execution time was reached. */
1480 if (elapsed >= g_pserver->lua_time_limit && g_pserver->lua_timedout == 0) {
1481 serverLog(LL_WARNING,
1482 "Lua slow script detected: still in execution after %lld milliseconds. "
1483 "You can try killing the script using the SCRIPT KILL command. "
1484 "Script SHA1 is: %s",
1485 elapsed, g_pserver->lua_cur_script);
1486 g_pserver->lua_timedout = 1;
1487 blockingOperationStarts();
1488 /* Once the script timeouts we reenter the event loop to permit others
1489 * to call SCRIPT KILL or SHUTDOWN NOSAVE if needed. For this reason
1490 * we need to mask the client executing the script from the event loop.
1491 * If we don't do that the client may disconnect and could no longer be
1492 * here when the EVAL command will return. */
1493 protectClient(g_pserver->lua_caller);
1494 }
1495 if (g_pserver->lua_timedout) processEventsWhileBlocked(serverTL - g_pserver->rgthreadvar);
1496 if (g_pserver->lua_kill) {
1497 serverLog(LL_WARNING,"Lua script killed by user with SCRIPT KILL.");
1498
1499 /*
1500 * Set the hook to invoke all the time so the user
1501         * will not be able to catch the error with pcall and invoke
1502         * pcall again which will prevent the script from ever been killed
1503 */
1504 lua_sethook(lua, luaMaskCountHook, LUA_MASKLINE, 0);
1505
1506 lua_pushstring(lua,"Script killed by user with SCRIPT KILL...");
1507 lua_error(lua);
1508 }
1509}
1510
1511void prepareLuaClient(void) {
1512 /* Select the right DB in the context of the Lua client */

Callers

nothing calls this directly

Calls 8

elapsedMsFunction · 0.85
serverLogFunction · 0.85
blockingOperationStartsFunction · 0.85
protectClientFunction · 0.85
lua_sethookFunction · 0.85
lua_pushstringFunction · 0.85
lua_errorFunction · 0.85

Tested by

no test coverage detected