Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

bug: tcp-logger attempts to send nil data and crashes when json encoding fails

Open Beginner friendly
#14,003 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

@bhuvan-somisetty is already working on this.

Since Oct 2, 2026.

  • #14004 by @bhuvan-somisetty — open

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

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

bug
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
  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
Dominant language
Lua
Stars
17.2k
Forks
3k
Avg merge
3d 15h
Merged PRs (30d)
23

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from apache/apisix

All issues in apache/apisix

Similar issues

More Lua issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.