bhuvan-somisetty opened a new issue, #14003:
URL: https://github.com/apache/apisix/issues/14003
### Current Behavior
In `apisix/plugins/tcp-logger.lua`, the batch processor callback serializes
log entries to JSON before transmission:
```lua
local func = function(entries, batch_max_size)
local data, err
if batch_max_size == 1 then
data, err = core.json.encode(entries[1]) -- encode as single {}
else
data, err = core.json.encode(entries) -- encode as array [{}]
end
if not data then
core.log.error('error occurred while encoding the data: ', err)
end
return send_tcp_data(conf, data)
end
```
When `core.json.encode()` fails (for example, due to invalid UTF-8 byte
sequences or non-serializable types in request/response headers or custom log
formats), `data` is `nil`. Instead of returning `false, "error occurred while
encoding the data: " .. err` back to the batch processor (as correctly handled
in `udp-logger.lua` and `rocketmq-logger.lua`), execution continues and calls
`send_tcp_data(conf, nil)`.
Inside `send_tcp_data()`:
```lua
ok, err = sock:send(log_message)
```
Because `log_message` is `nil`, OpenResty throws a Lua runtime exception:
`bad argument #1 to 'send' (string or table expected, got nil)`
Additionally, in `send_tcp_data()` at line 96:
```lua
return false, "failed to init the socket" .. soc_err
```
The error string runs together without a separator when socket
initialization fails (e.g. `failed to init the socketclosed`).
### Expected Behavior
When `core.json.encode()` fails to encode log entries, the callback should
immediately return `false, "error occurred while encoding the data: " .. err`
so the batch processor can record and handle the failure gracefully without
throwing an unhandled exception.
### Error Logs
```text
lua entry thread aborted: runtime error:
/usr/local/apisix/apisix/plugins/tcp-logger.lua:117: bad argument #1 to 'send'
(string or table expected, got nil)
stack traceback:
coroutine 0:
[C]: in function 'send'
/usr/local/apisix/apisix/plugins/tcp-logger.lua:117: in
function 'send_tcp_data'
/usr/local/apisix/apisix/plugins/tcp-logger.lua:163: in
function </usr/local/apisix/apisix/plugins/tcp-logger.lua:150>
```
### Steps to Reproduce
1. Configure a route with the `tcp-logger` plugin pointing to any TCP
endpoint.
2. Trigger a request where the resulting log entry cannot be JSON-encoded
(or mock `core.json.encode` to return nil).
3. When the batch processor flushes, inspect `logs/error.log`.
4. Observe the uncaught runtime error `bad argument #1 to 'send' (string or
table expected, got nil)` caused by passing `nil` to `sock:send()`.
### Environment
- APISIX version (run `apisix version`): 3.19.0 (master @ c6b2adc3)
- Operating system (run `uname -a`): Linux / POSIX
- OpenResty / Nginx version (run `openresty -V` or `nginx -V`): OpenResty
1.25+
- etcd version, if relevant: N/A
- APISIX Dashboard version, if relevant: N/A
- Plugin runner version, for issues related to plugin runners: N/A
- LuaRocks version, for installation issues: N/A
--
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]