BUG/MEDIUM: hlua: streams don't support mixing lua-load with lua-load-per-thread

Michel Mayen reported that mixing lua actions loaded from 'lua-load'
and 'lua-load-per-thread' directives within a single http/tcp session
yields unexpected results.

When executing action defined in another running context from the one of
the previously executed action (from lua-load, then from
lua-load-per-thread or the opposite, order doesn't matter), it would yield
this kind of error:

"Lua function 'name': [state-id x] runtime error: attempt to call a nil value from ."

He also noted that when loading all actions using the same loading
directive, the issue is gone.

This is due to the fact that for lua actions, fetches and converters, lua
code is being executed from the stream lua context. However, the stream
lua context, which is created on the fly when first executing some lua
code related to the stream, is reused between multiple lua executions.

But the thing is, despite successive executions referring to the same
parent "stream" (which is also assigned to a given thread id), they don't
necessarily depend on the same running context from lua point of view.

Indeed, since the function which is about to be executed could have been
loaded from either 'lua-load' or 'lua-load-per-thread', the function
declaration and related dependencies are defined in a specific stack ID
which is known by calling fcn_ref_to_stack_id() on the given function.

Thus, in order to make streams capable of chaining lua actions, fetches and
converters loaded in different lua stacks, we add a new detection logic
in hlua_stream_ctx_prepare() to be able to recreate the lua context in the
proper stack space when the existing one conflicts with the expected stack
id.

This must be backported in every stable versions.

It depends on:
 - "MINOR: hlua: add hlua_stream_prepare helper function"
   [for < 2.5, skip the filter part since they didn't exist]

[wt: warning, wait a little bit before backporting too far, we
 need to be certain the added BUG_ON() will never trigger]

(cherry picked from commit 09133860bf88d9abf77b107219d7d4fc50df9e08)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 0249366ec9f90817497bf805b0d1f1e196d5a85e)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 0c5e98b664228a1c7f7ff69cb2b7e21f68a39d9d)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit e638bca7e0a292d99cfe85064f5947035000771d)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
1 file changed