Skip to content

bug: runtime error with proxy-cache plugin when using in memory caching without specifying the memory_cache zone #9957

Description

@kayx23

Current Behavior

This issue summurizes #8719 which was reported for the same behaviour.

If you configure proxy-cache plugin with ONLY "cache_strategy": "memory", you get a 500 Internal Server Error

$ curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "uri": "/ip",
    "plugins": {
        "proxy-cache": {
            "cache_strategy": "memory",
            "cache_ttl": 10
        }
    },
    "upstream": {
        "nodes": {
            "httpbin.org": 1
        },
        "type": "roundrobin"
    }
}'
$ curl -i http://127.0.0.1:9080/ip
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>openresty</center>
<p><em>Powered by <a href="https://apisix.apache.org/">APISIX</a>.</em></p></body>
</html>

Logs:

[error] 128#128: *7943331 lua entry thread aborted: runtime error: ./apisix/plugins/proxy-cache/memory.lua:47: attempt to index field 'dict' (a nil value)
stack traceback:
coroutine 0:
        ./apisix/plugins/proxy-cache/memory.lua: in function 'get'
        ./apisix/plugins/proxy-cache/memory_handler.lua:204: in function 'phase_func'
        ./apisix/plugin.lua:1099: in function 'run_plugin'
        ./apisix/init.lua:674: in function 'http_access_phase'
        access_by_lua(nginx.conf:340):2: in main chunk, client: 192.168.112.1, server: _, request: "POST /api HTTP/1.1", host: "127.0.0.1:9080"

The correct usage is to configure "cache_strategy": "memory"with "cache_zone": "memory_cache" (or any other custom memory cache zones defined in config.yaml), such as the below:

"proxy-cache": {
      "cache_strategy": "memory",
      "cache_zone": "memory_cache"
}

Expected Behavior

If a user incorrectly configure just the "cache_strategy": "memory" without specifying a memory zone, the program should return an error message to help users identify the issue, instead of crashing like the current behaviour.

Suggested Fix

  • Add the error handler to return an error message to help users identify the issue, instead of crashing.
  • Update the proxy-cache doc. The current doc does not have an example for the in-memory cache_strategy, nor does it warn users of configuring the proper cache_zone for in-memory cache_strategy.

Environment

APISIX version (run apisix version): 3.4.0

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggood first issueGood for newcomers

Type

No type

Projects

Status
✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions