From c9d78382130691120a69329bc05e8b9563d93d03 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Fri, 5 Aug 2022 15:42:17 -0700 Subject: [PATCH] Enter meltdown mode if ProgramMaxWorkers triggers --- tool/net/help.txt | 8 ++++---- tool/net/redbean.c | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tool/net/help.txt b/tool/net/help.txt index fe9b83f5f..c3dd4c4b7 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -741,10 +741,10 @@ FUNCTIONS Turns Lua data structure into JSON string. - Since Lua uses tables are both hashmaps and arrays, we use a - simple fast algorithm for telling the two apart. Tables with - non-zero length (as reported by `#`) are encoded as arrays, - and any non-array elements are ignored. For example: + Since Lua tables are both hashmaps and arrays, we use a simple + fast algorithm for telling the two apart. Tables with non-zero + length (as reported by `#`) are encoded as arrays, and any + non-array elements are ignored. For example: >: EncodeJson({2}) "[2]" diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 43790cfe4..1c61afa3b 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -4755,6 +4755,7 @@ static int LuaProgramMaxWorkers(lua_State *L) { } lua_pushinteger(L, maxworkers); if (lua_isinteger(L, 1)) maxworkers = lua_tointeger(L, 1); + maxworkers = MAX(maxworkers, 1); return 1; } @@ -6749,6 +6750,12 @@ static int HandleConnection(size_t i) { if ((client = accept4(servers.p[i].fd, &clientaddr, &clientaddrsize, SOCK_CLOEXEC)) != -1) { startconnection = nowl(); + if (UNLIKELY(maxworkers) && shared->workers >= maxworkers) { + EnterMeltdownMode(); + SendServiceUnavailable(); + close(client); + return 0; + } VERBOSEF("(srvr) accept %s via %s", DescribeClient(), DescribeServer()); messageshandled = 0; if (hasonclientconnection && LuaOnClientConnection()) { @@ -6939,7 +6946,6 @@ static int HandlePoll(int ms) { if (!polls[pollid].revents) continue; if (polls[pollid].fd < 0) continue; if (polls[pollid].fd) { - if (maxworkers && shared->workers >= maxworkers) continue; // handle listen socket lua_repl_lock(); serverid = pollid - 1;