nic-6443 opened a new pull request, #13258:
URL: https://github.com/apache/apisix/pull/13258
### What this does
Fix a crash in the Consul service discovery worker when a registered service
has no metadata.
### Why
When `consul services register` is used without `meta`, the agent's catalog
API returns `"Meta": null`. cjson decodes JSON null as a userdata sentinel
(`cjson.null`), which is truthy but not a table. The current guard at
`apisix/discovery/consul/client.lua:415`:
```lua
if preserve_metadata and node.Service.Meta
and next(node.Service.Meta) then
```
passes the truthiness check, then calls `next()` on userdata and aborts the
entire catalog scan with:
```
bad argument #1 to 'next' (table expected, got userdata)
```
The discovery worker never recovers — no nodes are produced for any service
in that response, so all upstreams resolved through Consul end up empty until
the worker is restarted (and even then it crashes again on the next scrape).
This was introduced by #13230 which extracted `client.lua` and added the new
`preserve_metadata` path; older code didn't iterate `Service.Meta`.
### Fix
Use `type(node.Service.Meta) == "table"` instead of relying on truthiness,
so `cjson.null` is treated as "no metadata" (same as the field being absent).
### Test
Added a regression test (`t/discovery/consul.t` TEST 14) that:
1. registers a real consul service without `Meta`
2. calls `consul_client.fetch_services_from_server` directly with
`preserve_metadata=true`
3. asserts no error and `metadata == nil`
The test fails on master with the userdata error and passes with the fix.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]