]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MEDIUM: hlua: Fix receive API for TCP applets to properly handle shutdowns
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 26 May 2025 16:11:19 +0000 (18:11 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 27 May 2025 05:53:19 +0000 (07:53 +0200)
An optional timeout was added to AppletTCP.receive() to interrupt calls after a
delay. It was mandatory to be able to implement interactive applets (like
trisdemo). However, this broke the API and it made impossible to differentiate
the shutdowns from the delays expirations. Indeed, in both cases, an empty
string was returned.

Because historically an empty string was used to notify a connection shutdown,
it should not be changed. So now, 'nil' value is returned when no data was
available before the delay expiration.

The new AppletTCP:try_receive() function was also affected. To fix it, instead
of stating there is no delay when a receive is tried, an expired delay is
set. Concretely TICK_ETERNITY was replaced by now_ms.

Finally, AppletTCP:getline() function is not concerned for now because there
is no way to interrupt it after some delay.

The documentation and trisdemo lua script were updated accordingly.

This patch depends on "BUG/MEDIUM: hlua: Properly detect shudowns for TCP
applets based on the new API". However, it is a 3.2-specific issue, so no
backport is needed.

doc/lua-api/index.rst
examples/lua/trisdemo.lua
src/hlua.c

index b9edce62b9b89c592790739fad9914466c15236c..e8d494b02eb1bfe3dbe7019ea3ac72caab87960e 100644 (file)
@@ -3916,21 +3916,25 @@ AppletTCP class
   *size* is missing, the function tries to read all the content of the stream
   until the end. An optional timeout may be specified in milliseconds. In this
   case the function will return no longer than this delay, with the amount of
-  available data (possibly none).
+  available data, or nil if there is no data. An empty string is returned if the
+  connection is closed.
 
   :param class_AppletTCP applet: An :ref:`applettcp_class`
   :param integer size: the required read size.
-  :returns: always return a string, the string can be empty if the connection is
-   closed.
+  :returns: return nil if the timeout has expired and no data was available but
+   can still be received. Otherwise, a string is returned, possibly an empty
+   string if the connection is closed.
 
 .. js:function:: AppletTCP.try_receive(applet)
 
   Reads available data from the TCP stream and returns immediately. Returns a
-  string containing read bytes that may possibly be empty if no bytes are
-  available at that time.
+  string containing read bytes or nil if no bytes are available at that time. An
+  empty string is returned if the connection is closed.
 
   :param class_AppletTCP applet: An :ref:`applettcp_class`
-  :returns: always return a string, the string can be empty.
+  :returns: return nil if no data was available but can still be
+   received. Otherwise, a string is returned, possibly an empty string if the
+   connection is closed.
 
 .. js:function:: AppletTCP.send(appletmsg)
 
index 4b60bb5ac218e1d12190d8b5874be08b9bb451a0..e591b996a3c9f045fa03b3c8c0a2b735ed537076 100644 (file)
@@ -215,7 +215,7 @@ function handler(applet)
 
         local input = applet:receive(1, delay)
         if input then
-            if input == "q" then
+            if input == "" or input == "q" then
                 game_over = true
             elseif input == "\27" then
                 local a = applet:receive(1, delay)
index 6c61762e971929e973e5595677b8d4621f6edb56..bdf31da76abc21aee910758e75505467eedd6c9d 100644 (file)
@@ -5405,7 +5405,7 @@ __LJMP static int hlua_applet_tcp_recv_try(lua_State *L)
        if (ret == 0) {
                if (tick_is_expired(exp_date, now_ms)) {
                        /* return the result. */
-                       luaL_pushresult(&luactx->b);
+                       lua_pushnil(L);
                        return 1;
                }
 
@@ -5545,7 +5545,7 @@ __LJMP static int hlua_applet_tcp_try_recv(lua_State *L)
        lua_pushinteger(L, -1);
 
        /* set the expiration date (mandatory arg but not relevant here) */
-       lua_pushinteger(L, TICK_ETERNITY);
+       lua_pushinteger(L, now_ms);
 
        /* Initialise the string catenation. */
        luaL_buffinit(L, &luactx->b);