fix: replace module-level mutable tables with per-call allocation#13369
Merged
nic-6443 merged 3 commits intoMay 14, 2026
Conversation
Several plugins and routers declared `local tbl = {}` at module scope
and reused the same table across coroutines via `core.table.clear()`.
When a function yields (e.g. `ctx.var[...]` lookup or shdict op),
another concurrent request on the same worker can re-enter and mutate
the shared table, causing cross-request state pollution.
Affected high-risk sites (yield in function body):
- prometheus/exporter.lua: inner_tab_arr in gen_arr(), extra_labels_tbl
in extra_labels() — use per-call local table
- proxy-cache/util.lua: tmp in generate_complex_value() — use tablepool
- redirect.lua: tmp in concat_new_uri() — use tablepool
Affected low-risk sites (preventive, no current yield):
- api_router.lua: match_opts in match() — use tablepool
- control/router.lua: match_opts in match() — use tablepool
- stream/router/ip_port.lua: match_opts in match() — use tablepool
This follows the same fix pattern as the historical radixtree_host_uri
route-mismatch fix (PR apache#10198).
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR removes module-level mutable tables that were reused across concurrent requests (coroutines) and replaces them with per-call allocations or core.tablepool.fetch/release to prevent cross-request state pollution when functions yield.
Changes:
- Replaced module-scope
match_optstables withcore.tablepool.fetch/releasein multiple routers. - Replaced module-scope temporary arrays in redirect/proxy-cache utilities with
core.tablepool.fetch/release. - Replaced reuse of module-scope tables in Prometheus exporter helpers with per-call table allocations.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| apisix/stream/router/ip_port.lua | Uses tablepool for match_opts during TLS router dispatch. |
| apisix/plugins/redirect.lua | Uses tablepool for URI segment concatenation temp table. |
| apisix/plugins/proxy-cache/util.lua | Uses tablepool for complex value concatenation temp table. |
| apisix/plugins/prometheus/exporter.lua | Switches helper tables to per-call allocations to avoid shared state. |
| apisix/control/router.lua | Uses tablepool for match_opts during control-plane route dispatch. |
| apisix/api_router.lua | Uses tablepool for match_opts during API router dispatch. |
Comments suppressed due to low confidence (2)
apisix/plugins/redirect.lua:1
tmpis released only on the normal path. If an exception is raised anywhere afterfetch(e.g., unexpected runtime error inside the loop/body), the pooled table will not be returned to the pool, which can degrade long-running worker behavior. Consider wrapping the body betweenfetchandreleasesocore.tablepool.release(...)is guaranteed to run even when an error occurs (then rethrow/propagate the error).
--
apisix/plugins/proxy-cache/util.lua:1
- Same pooling concern as in
redirect.lua: if any error occurs afterfetch(e.g., fromresolve_varor other runtime failures),tmpwill not be released back to the pool. Ensurecore.tablepool.release(...)executes on both success and error paths to avoid leaking pooled tables.
--
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Nic <[email protected]>
membphis
approved these changes
May 14, 2026
shreemaan-abhishek
approved these changes
May 14, 2026
AlinsRan
approved these changes
May 14, 2026
wistefan
pushed a commit
to wistefan/apisix
that referenced
this pull request
Jun 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Several plugins and routers declared
local tbl = {}at module scope and reused the same table across coroutines viacore.table.clear(). When a function yields (e.g.ctx.var[...]lookup or shdict op), another concurrent request on the same worker can re-enter and mutate the shared table, causing cross-request state pollution.Affected high-risk sites (yield in function body):
prometheus/exporter.lua:inner_tab_arringen_arr(),extra_labels_tblinextra_labels()— replaced with per-call local tableproxy-cache/util.lua:tmpingenerate_complex_value()— replaced withcore.tablepool.fetch/releaseredirect.lua:tmpinconcat_new_uri()— replaced withcore.tablepool.fetch/releaseAffected low-risk sites (preventive, no current yield):
api_router.lua:match_optsinmatch()— replaced withcore.tablepool.fetch/releasecontrol/router.lua:match_optsinmatch()— replaced withcore.tablepool.fetch/releasestream/router/ip_port.lua:match_optsinmatch()— replaced withcore.tablepool.fetch/releaseThis follows the same fix pattern as the historical radixtree_host_uri route-mismatch fix (PR #10198).