Skip to content

fix(tcp-logger): return error when json encoding fails in batch processor - #14004

Open
bhuvan-somisetty wants to merge 1 commit into
apache:masterfrom
bhuvan-somisetty:fix/tcp-logger-nil-data-crash
Open

bhuvan-somisetty wants to merge 1 commit into
apache:masterfrom
bhuvan-somisetty:fix/tcp-logger-nil-data-crash

Conversation

@bhuvan-somisetty

Copy link
Copy Markdown

Description

In apisix/plugins/tcp-logger.lua, when core.json.encode() fails to serialize log entries, the callback was logging the error but proceeding to call send_tcp_data(conf, nil). This resulted in sock:send(nil) throwing an unhandled Lua exception (bad argument #1 to 'send' (string or table expected, got nil)).

This change:

  1. Returns false, 'error occurred while encoding the data: ' .. err early when JSON serialization fails, aligning with udp-logger and rocketmq-logger.
  2. Adds a colon separator to the socket initialization error message ("failed to init the socket: " .. soc_err).
  3. Adds test coverage for JSON encoding failure handling in t/plugin/tcp-logger.t.

Which issue(s) this PR fixes:

Fixes #14003

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

…ssor

When core.json.encode() failed in the batch processor callback, tcp-logger
logged the error but continued execution by calling send_tcp_data(conf, nil).
This caused OpenResty's sock:send() to crash with a runtime error:
"bad argument apache#1 to 'send' (string or table expected, got nil)".

Return `false, 'error occurred while encoding the data: ' .. err` early,
matching udp-logger and rocketmq-logger, and add a colon separator in the
socket initialization error message.

Fixes apache#14003

Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant