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

Aperta Adatta ai principianti
#14,003 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@LiteSun ci sta già lavorando.

Dal 29/10/2026.

  • #14004 di @bhuvan-somisetty — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
lua

Direzione di ricerca

Inizia da apisix/plugins/tcp-logger.lua, concentrandoti sul callback del processore batch e su send_tcp_data(). Confronta la gestione degli errori di codifica con udp-logger.lua e rocketmq-logger.lua e controlla il messaggio di inizializzazione del socket. Il lavoro è completato quando gli errori di codifica restituiscono l'errore indicato senza chiamare send_tcp_data con nil e gli errori di inizializzazione del socket includono un separatore.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Lua
Stelle
17.3k
Fork
3k
Merge medio
3g 6h
PR unite (30g)
68

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di apache/apisix

Tutte le issue di apache/apisix

Issue simili

Altre issue su Lua

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.