bug: tcp-logger attempts to send nil data and crashes when json encoding fails
Maintainers usually reply within 3 days
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- lua
- Domain
- api, backend, observability
Research direction
Start in apisix/plugins/tcp-logger.lua, focusing on the batch processor callback and send_tcp_data(). Compare the encoding-error handling with udp-logger.lua and rocketmq-logger.lua, and inspect the socket initialization message. Done means encoding failures return the stated error without calling send_tcp_data with nil, and socket initialization errors include a separator.
Written by the indexing model from the issue text.
Description
Current Behavior
In apisix/plugins/tcp-logger.lua, the batch processor callback serializes log entries to JSON before transmission:
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():
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:
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
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
- Configure a route with the
tcp-loggerplugin pointing to any TCP endpoint. - Trigger a request where the resulting log entry cannot be JSON-encoded (or mock
core.json.encodeto return nil). - When the batch processor flushes, inspect
logs/error.log. - Observe the uncaught runtime error
bad argument #1 to 'send' (string or table expected, got nil)caused by passingniltosock:send().
Environment
- APISIX version (run
apisix version): 3.19.0 (master @ c6b2adc3) - Operating system (run
uname -a): Linux / POSIX - OpenResty / Nginx version (run
openresty -Vornginx -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
- Dominant language
- Lua
- Stars
- 17.2k
- Forks
- 3k
- Avg merge
- 3d 15h
- Merged PRs (30d)
- 23
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from apache/apisix
-
bug: splunk-hec-logging crashes with nil concatenation when error response json lacks text fieldPossibly taken @bhuvan-somisetty claimed this 10 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
apache/apisix#14011 · 1 assignee ·
Maintainers usually reply within 3 days
-
bug: datadog plugin drops service_name tag when service has no name configuredPossibly taken @bhuvan-somisetty claimed this 17 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 3 days
-
bug: syslog error messages run together with the underlying error (missing separator)Possibly taken @bhuvan-somisetty claimed this 27 days ago. Openbug
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 3 days
-
bug: syslog plugin crashes with nil-index error instead of returning an error when logger init failsPossibly taken @bhuvan-somisetty claimed this 30 days ago. Openbug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 3 days
-
bug: mqtt-proxy stream plugin crashes on malformed MQTT v5 properties lengthPossibly taken @bhuvan-somisetty claimed this 32 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 3 days
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
windwp/nvim-ts-autotag#266 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
pterodactyl/game-eggs#657 ·
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
stalker57241/tabview#10 ·
-
feature-request mini.icons
Difficulty 1/5 Under an hour Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
mfussenegger/nvim-dap#1613 · 2 comments ·