From d73b465c6b60be9057cc156d6f82060e60d2a4cf Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Sun, 14 May 2023 01:05:36 -0700 Subject: [PATCH] Updated redbean Fetch to remove open connections on error --- tool/net/fetch.inc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tool/net/fetch.inc b/tool/net/fetch.inc index a018c5d8f..9260a8275 100644 --- a/tool/net/fetch.inc +++ b/tool/net/fetch.inc @@ -185,12 +185,13 @@ static int LuaFetch(lua_State *L) { if (lua_isinteger(L, -1)) { sock = lua_tointeger(L, -1); keepalive = lua_toboolean(L, -2) ? kaCLOSE : kaKEEP; + // remove host mapping, as the socket is ether being closed + // (so needs to be removed) or will be added after the request is done; + // this also helps to keep the mapping clean in case of an error + lua_pushnil(L); // aft: -4=tbl, -3=close, -2=hosthdr, -1=nil + lua_setfield(L, -4, hosthdr); VERBOSEF("(ftch) reuse socket %d for host %s (and %s)", sock, hosthdr, keepalive == kaCLOSE ? "close" : "keep"); - if (keepalive == kaCLOSE) { // remove socket being closed - lua_pushnil(L); // aft: -4=tbl, -3=close, -2=hosthdr, -1=nil - lua_setfield(L, -4, hosthdr); - } } lua_settop(L, 2); // drop all added elements to keep the stack balanced }